Deleting folders that have serialized names in Powershell












0















My problem maymay not be complicated but I have been scratching my head and searching for a way to do this but so far have not come up with much.



I have a folder structure like so:



C:
└───ParentFolder
├───ChildFolder1
│ ├───SubFolderA_1
│ ├───SubFolderA_2
│ ├───SubFolderA_3
│ ├───SubFolderA_4
│ ├───SubFolderB_1
│ ├───SubFolderB_2
│ ├───SubFolderB_3
│ └───SubFolderB_4
└───ChildFolder2
├───SubFolderA_1
├───SubFolderA_2
├───SubFolderA_3
├───SubFolderA_4
├───SubFolderB_1
├───SubFolderB_2
├───SubFolderB_3
└───SubFolderB_4


What I'm looking for is a PowerShell script that would utilize the serialized nature of the "SubFolders" names to delete older versions, leaving only the most recent SubFolders in place.



Using the example above, this would mean the script would delete SubFolderA_1 to SubFolderA_3 and SubFolderB_1 to SubFolderB_3, leaving only SubFolderA_4 and SubfolderB_4 in the ChildFolders.



Would anyone know a way of doing this? I was thinking about Object Sorting + The Recursive Function + pattern matching, but I don't seem to get anywhere with it. I'm a PS noob by the way.



Your help would be much appreciated.










share|improve this question

























  • Hello, it's nice that You have Joined StackOverflow community. When asking questions - please follow instructions from below link so that we can help You effectively. Please let us know what dud You try so far, and we will help You build on that. stackoverflow.com/help/how-to-ask

    – Tomek
    Jan 4 at 8:34
















0















My problem maymay not be complicated but I have been scratching my head and searching for a way to do this but so far have not come up with much.



I have a folder structure like so:



C:
└───ParentFolder
├───ChildFolder1
│ ├───SubFolderA_1
│ ├───SubFolderA_2
│ ├───SubFolderA_3
│ ├───SubFolderA_4
│ ├───SubFolderB_1
│ ├───SubFolderB_2
│ ├───SubFolderB_3
│ └───SubFolderB_4
└───ChildFolder2
├───SubFolderA_1
├───SubFolderA_2
├───SubFolderA_3
├───SubFolderA_4
├───SubFolderB_1
├───SubFolderB_2
├───SubFolderB_3
└───SubFolderB_4


What I'm looking for is a PowerShell script that would utilize the serialized nature of the "SubFolders" names to delete older versions, leaving only the most recent SubFolders in place.



Using the example above, this would mean the script would delete SubFolderA_1 to SubFolderA_3 and SubFolderB_1 to SubFolderB_3, leaving only SubFolderA_4 and SubfolderB_4 in the ChildFolders.



Would anyone know a way of doing this? I was thinking about Object Sorting + The Recursive Function + pattern matching, but I don't seem to get anywhere with it. I'm a PS noob by the way.



Your help would be much appreciated.










share|improve this question

























  • Hello, it's nice that You have Joined StackOverflow community. When asking questions - please follow instructions from below link so that we can help You effectively. Please let us know what dud You try so far, and we will help You build on that. stackoverflow.com/help/how-to-ask

    – Tomek
    Jan 4 at 8:34














0












0








0








My problem maymay not be complicated but I have been scratching my head and searching for a way to do this but so far have not come up with much.



I have a folder structure like so:



C:
└───ParentFolder
├───ChildFolder1
│ ├───SubFolderA_1
│ ├───SubFolderA_2
│ ├───SubFolderA_3
│ ├───SubFolderA_4
│ ├───SubFolderB_1
│ ├───SubFolderB_2
│ ├───SubFolderB_3
│ └───SubFolderB_4
└───ChildFolder2
├───SubFolderA_1
├───SubFolderA_2
├───SubFolderA_3
├───SubFolderA_4
├───SubFolderB_1
├───SubFolderB_2
├───SubFolderB_3
└───SubFolderB_4


What I'm looking for is a PowerShell script that would utilize the serialized nature of the "SubFolders" names to delete older versions, leaving only the most recent SubFolders in place.



Using the example above, this would mean the script would delete SubFolderA_1 to SubFolderA_3 and SubFolderB_1 to SubFolderB_3, leaving only SubFolderA_4 and SubfolderB_4 in the ChildFolders.



Would anyone know a way of doing this? I was thinking about Object Sorting + The Recursive Function + pattern matching, but I don't seem to get anywhere with it. I'm a PS noob by the way.



Your help would be much appreciated.










share|improve this question
















My problem maymay not be complicated but I have been scratching my head and searching for a way to do this but so far have not come up with much.



I have a folder structure like so:



C:
└───ParentFolder
├───ChildFolder1
│ ├───SubFolderA_1
│ ├───SubFolderA_2
│ ├───SubFolderA_3
│ ├───SubFolderA_4
│ ├───SubFolderB_1
│ ├───SubFolderB_2
│ ├───SubFolderB_3
│ └───SubFolderB_4
└───ChildFolder2
├───SubFolderA_1
├───SubFolderA_2
├───SubFolderA_3
├───SubFolderA_4
├───SubFolderB_1
├───SubFolderB_2
├───SubFolderB_3
└───SubFolderB_4


What I'm looking for is a PowerShell script that would utilize the serialized nature of the "SubFolders" names to delete older versions, leaving only the most recent SubFolders in place.



Using the example above, this would mean the script would delete SubFolderA_1 to SubFolderA_3 and SubFolderB_1 to SubFolderB_3, leaving only SubFolderA_4 and SubfolderB_4 in the ChildFolders.



Would anyone know a way of doing this? I was thinking about Object Sorting + The Recursive Function + pattern matching, but I don't seem to get anywhere with it. I'm a PS noob by the way.



Your help would be much appreciated.







windows powershell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 14:09









LotPings

19.9k61633




19.9k61633










asked Jan 2 at 13:00









DustreeDustree

31




31













  • Hello, it's nice that You have Joined StackOverflow community. When asking questions - please follow instructions from below link so that we can help You effectively. Please let us know what dud You try so far, and we will help You build on that. stackoverflow.com/help/how-to-ask

    – Tomek
    Jan 4 at 8:34



















  • Hello, it's nice that You have Joined StackOverflow community. When asking questions - please follow instructions from below link so that we can help You effectively. Please let us know what dud You try so far, and we will help You build on that. stackoverflow.com/help/how-to-ask

    – Tomek
    Jan 4 at 8:34

















Hello, it's nice that You have Joined StackOverflow community. When asking questions - please follow instructions from below link so that we can help You effectively. Please let us know what dud You try so far, and we will help You build on that. stackoverflow.com/help/how-to-ask

– Tomek
Jan 4 at 8:34





Hello, it's nice that You have Joined StackOverflow community. When asking questions - please follow instructions from below link so that we can help You effectively. Please let us know what dud You try so far, and we will help You build on that. stackoverflow.com/help/how-to-ask

– Tomek
Jan 4 at 8:34












2 Answers
2






active

oldest

votes


















0














Provided there is only one underscore,




  • list folders with multiple wildcards

  • split the fullname at the underscore and

  • Group by the first part,

  • iterate the groups,

  • sort descending (if there may be diversing places convert to an int or use $ToNatural) and

  • skip the first

  • remove remaining folders.


This is quite similar to Lee_Daileys script but will sort with all numbers padded left with zeros to a unique width.



## $ToNatural from Roman Kuzmin source https://stackoverflow.com/a/5429048/6811411
$ToNatural = { [regex]::Replace($_, 'd+', { $args[0].Value.PadLeft(20,"0") }) }

Get-ChildItem C:ParentFolderChildFolder*Subfolder* |
Group-Object @{e={$_.fullname.split('_')[0]}} | ForEach-Object {
$_.Group | Sort $ToNatural -Descending | Select -Skip 1 | Remove-Item -WhatIf
}


If the output looks OK, remove the trailing -WhatIf






share|improve this answer





















  • 1





    Much appreciated LotPings, this works extremely well. I just needed to add "-recurse -force -confirm:$false" to the Remove-Item function to get around the constantly presented prompt "The item at <Directory Path> has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):"

    – Dustree
    Jan 2 at 16:18



















1














here's one way to do it. [grin] the heart of it is the Group-Object cmdlet. one thing often overlooked about it is the ability to use a calculated property much as can be done with the Select-Object cmdlet.



# fake reading in a list of directories
# in real life, use Get-ChildItem -Directory
$DirList = @(
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_11'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_22'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_44'
)

$GroupedDirList = $DirList |
# changed from sorting by the FullName to sorting by the trailing number
# thanks to LotPings for pointing out the glitch with multi-digit numbers
Sort-Object {[int]$_.FullName.Split('_')[1]} |
Group-Object {$_.FullName.Split('_')[0]}

foreach ($GDL_Item in $GroupedDirList)
{
$GDL_Item.Group |
Select-Object -SkipLast 1 |
ForEach-Object {
# remove the quotes, the Write-Host, and the "$()" when you do this for real
# can't use the "-WhatIf" parameter here since the dirs don't actually exist on my system
Write-Host "Remove-Item -LiteralPath $($_.FullName) -Recurse -WhatIf"
}

'=' * 20
}


output ...



Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_3 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_11 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_22 -Recurse -WhatIf
====================





share|improve this answer





















  • 1





    Nice one(+1) was about to post a similar script. But what happens if the trailing count exceeds 9/one place ?

    – LotPings
    Jan 2 at 14:08













  • @LotPings - hah! very good point ... i forgot that Sort-Object would be doing a string sort without any awareness of numbers. [blush] i'm off to fix that ...

    – Lee_Dailey
    Jan 2 at 14:19






  • 1





    Many thanks Lee_Dailey. I have also tried your script and this works perfectly with the addition of "-recurse -force -confirm:$false". to remove Item. Thanks again.

    – Dustree
    Jan 2 at 17:17













  • @Dustree - you are most welcome! glad to have helped a little ... [grin]

    – Lee_Dailey
    Jan 2 at 17:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006850%2fdeleting-folders-that-have-serialized-names-in-powershell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Provided there is only one underscore,




  • list folders with multiple wildcards

  • split the fullname at the underscore and

  • Group by the first part,

  • iterate the groups,

  • sort descending (if there may be diversing places convert to an int or use $ToNatural) and

  • skip the first

  • remove remaining folders.


This is quite similar to Lee_Daileys script but will sort with all numbers padded left with zeros to a unique width.



## $ToNatural from Roman Kuzmin source https://stackoverflow.com/a/5429048/6811411
$ToNatural = { [regex]::Replace($_, 'd+', { $args[0].Value.PadLeft(20,"0") }) }

Get-ChildItem C:ParentFolderChildFolder*Subfolder* |
Group-Object @{e={$_.fullname.split('_')[0]}} | ForEach-Object {
$_.Group | Sort $ToNatural -Descending | Select -Skip 1 | Remove-Item -WhatIf
}


If the output looks OK, remove the trailing -WhatIf






share|improve this answer





















  • 1





    Much appreciated LotPings, this works extremely well. I just needed to add "-recurse -force -confirm:$false" to the Remove-Item function to get around the constantly presented prompt "The item at <Directory Path> has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):"

    – Dustree
    Jan 2 at 16:18
















0














Provided there is only one underscore,




  • list folders with multiple wildcards

  • split the fullname at the underscore and

  • Group by the first part,

  • iterate the groups,

  • sort descending (if there may be diversing places convert to an int or use $ToNatural) and

  • skip the first

  • remove remaining folders.


This is quite similar to Lee_Daileys script but will sort with all numbers padded left with zeros to a unique width.



## $ToNatural from Roman Kuzmin source https://stackoverflow.com/a/5429048/6811411
$ToNatural = { [regex]::Replace($_, 'd+', { $args[0].Value.PadLeft(20,"0") }) }

Get-ChildItem C:ParentFolderChildFolder*Subfolder* |
Group-Object @{e={$_.fullname.split('_')[0]}} | ForEach-Object {
$_.Group | Sort $ToNatural -Descending | Select -Skip 1 | Remove-Item -WhatIf
}


If the output looks OK, remove the trailing -WhatIf






share|improve this answer





















  • 1





    Much appreciated LotPings, this works extremely well. I just needed to add "-recurse -force -confirm:$false" to the Remove-Item function to get around the constantly presented prompt "The item at <Directory Path> has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):"

    – Dustree
    Jan 2 at 16:18














0












0








0







Provided there is only one underscore,




  • list folders with multiple wildcards

  • split the fullname at the underscore and

  • Group by the first part,

  • iterate the groups,

  • sort descending (if there may be diversing places convert to an int or use $ToNatural) and

  • skip the first

  • remove remaining folders.


This is quite similar to Lee_Daileys script but will sort with all numbers padded left with zeros to a unique width.



## $ToNatural from Roman Kuzmin source https://stackoverflow.com/a/5429048/6811411
$ToNatural = { [regex]::Replace($_, 'd+', { $args[0].Value.PadLeft(20,"0") }) }

Get-ChildItem C:ParentFolderChildFolder*Subfolder* |
Group-Object @{e={$_.fullname.split('_')[0]}} | ForEach-Object {
$_.Group | Sort $ToNatural -Descending | Select -Skip 1 | Remove-Item -WhatIf
}


If the output looks OK, remove the trailing -WhatIf






share|improve this answer















Provided there is only one underscore,




  • list folders with multiple wildcards

  • split the fullname at the underscore and

  • Group by the first part,

  • iterate the groups,

  • sort descending (if there may be diversing places convert to an int or use $ToNatural) and

  • skip the first

  • remove remaining folders.


This is quite similar to Lee_Daileys script but will sort with all numbers padded left with zeros to a unique width.



## $ToNatural from Roman Kuzmin source https://stackoverflow.com/a/5429048/6811411
$ToNatural = { [regex]::Replace($_, 'd+', { $args[0].Value.PadLeft(20,"0") }) }

Get-ChildItem C:ParentFolderChildFolder*Subfolder* |
Group-Object @{e={$_.fullname.split('_')[0]}} | ForEach-Object {
$_.Group | Sort $ToNatural -Descending | Select -Skip 1 | Remove-Item -WhatIf
}


If the output looks OK, remove the trailing -WhatIf







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 16:17

























answered Jan 2 at 14:20









LotPingsLotPings

19.9k61633




19.9k61633








  • 1





    Much appreciated LotPings, this works extremely well. I just needed to add "-recurse -force -confirm:$false" to the Remove-Item function to get around the constantly presented prompt "The item at <Directory Path> has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):"

    – Dustree
    Jan 2 at 16:18














  • 1





    Much appreciated LotPings, this works extremely well. I just needed to add "-recurse -force -confirm:$false" to the Remove-Item function to get around the constantly presented prompt "The item at <Directory Path> has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):"

    – Dustree
    Jan 2 at 16:18








1




1





Much appreciated LotPings, this works extremely well. I just needed to add "-recurse -force -confirm:$false" to the Remove-Item function to get around the constantly presented prompt "The item at <Directory Path> has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):"

– Dustree
Jan 2 at 16:18





Much appreciated LotPings, this works extremely well. I just needed to add "-recurse -force -confirm:$false" to the Remove-Item function to get around the constantly presented prompt "The item at <Directory Path> has children and the Recurse parameter was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue? [Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):"

– Dustree
Jan 2 at 16:18













1














here's one way to do it. [grin] the heart of it is the Group-Object cmdlet. one thing often overlooked about it is the ability to use a calculated property much as can be done with the Select-Object cmdlet.



# fake reading in a list of directories
# in real life, use Get-ChildItem -Directory
$DirList = @(
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_11'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_22'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_44'
)

$GroupedDirList = $DirList |
# changed from sorting by the FullName to sorting by the trailing number
# thanks to LotPings for pointing out the glitch with multi-digit numbers
Sort-Object {[int]$_.FullName.Split('_')[1]} |
Group-Object {$_.FullName.Split('_')[0]}

foreach ($GDL_Item in $GroupedDirList)
{
$GDL_Item.Group |
Select-Object -SkipLast 1 |
ForEach-Object {
# remove the quotes, the Write-Host, and the "$()" when you do this for real
# can't use the "-WhatIf" parameter here since the dirs don't actually exist on my system
Write-Host "Remove-Item -LiteralPath $($_.FullName) -Recurse -WhatIf"
}

'=' * 20
}


output ...



Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_3 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_11 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_22 -Recurse -WhatIf
====================





share|improve this answer





















  • 1





    Nice one(+1) was about to post a similar script. But what happens if the trailing count exceeds 9/one place ?

    – LotPings
    Jan 2 at 14:08













  • @LotPings - hah! very good point ... i forgot that Sort-Object would be doing a string sort without any awareness of numbers. [blush] i'm off to fix that ...

    – Lee_Dailey
    Jan 2 at 14:19






  • 1





    Many thanks Lee_Dailey. I have also tried your script and this works perfectly with the addition of "-recurse -force -confirm:$false". to remove Item. Thanks again.

    – Dustree
    Jan 2 at 17:17













  • @Dustree - you are most welcome! glad to have helped a little ... [grin]

    – Lee_Dailey
    Jan 2 at 17:44
















1














here's one way to do it. [grin] the heart of it is the Group-Object cmdlet. one thing often overlooked about it is the ability to use a calculated property much as can be done with the Select-Object cmdlet.



# fake reading in a list of directories
# in real life, use Get-ChildItem -Directory
$DirList = @(
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_11'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_22'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_44'
)

$GroupedDirList = $DirList |
# changed from sorting by the FullName to sorting by the trailing number
# thanks to LotPings for pointing out the glitch with multi-digit numbers
Sort-Object {[int]$_.FullName.Split('_')[1]} |
Group-Object {$_.FullName.Split('_')[0]}

foreach ($GDL_Item in $GroupedDirList)
{
$GDL_Item.Group |
Select-Object -SkipLast 1 |
ForEach-Object {
# remove the quotes, the Write-Host, and the "$()" when you do this for real
# can't use the "-WhatIf" parameter here since the dirs don't actually exist on my system
Write-Host "Remove-Item -LiteralPath $($_.FullName) -Recurse -WhatIf"
}

'=' * 20
}


output ...



Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_3 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_11 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_22 -Recurse -WhatIf
====================





share|improve this answer





















  • 1





    Nice one(+1) was about to post a similar script. But what happens if the trailing count exceeds 9/one place ?

    – LotPings
    Jan 2 at 14:08













  • @LotPings - hah! very good point ... i forgot that Sort-Object would be doing a string sort without any awareness of numbers. [blush] i'm off to fix that ...

    – Lee_Dailey
    Jan 2 at 14:19






  • 1





    Many thanks Lee_Dailey. I have also tried your script and this works perfectly with the addition of "-recurse -force -confirm:$false". to remove Item. Thanks again.

    – Dustree
    Jan 2 at 17:17













  • @Dustree - you are most welcome! glad to have helped a little ... [grin]

    – Lee_Dailey
    Jan 2 at 17:44














1












1








1







here's one way to do it. [grin] the heart of it is the Group-Object cmdlet. one thing often overlooked about it is the ability to use a calculated property much as can be done with the Select-Object cmdlet.



# fake reading in a list of directories
# in real life, use Get-ChildItem -Directory
$DirList = @(
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_11'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_22'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_44'
)

$GroupedDirList = $DirList |
# changed from sorting by the FullName to sorting by the trailing number
# thanks to LotPings for pointing out the glitch with multi-digit numbers
Sort-Object {[int]$_.FullName.Split('_')[1]} |
Group-Object {$_.FullName.Split('_')[0]}

foreach ($GDL_Item in $GroupedDirList)
{
$GDL_Item.Group |
Select-Object -SkipLast 1 |
ForEach-Object {
# remove the quotes, the Write-Host, and the "$()" when you do this for real
# can't use the "-WhatIf" parameter here since the dirs don't actually exist on my system
Write-Host "Remove-Item -LiteralPath $($_.FullName) -Recurse -WhatIf"
}

'=' * 20
}


output ...



Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_3 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_11 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_22 -Recurse -WhatIf
====================





share|improve this answer















here's one way to do it. [grin] the heart of it is the Group-Object cmdlet. one thing often overlooked about it is the ability to use a calculated property much as can be done with the Select-Object cmdlet.



# fake reading in a list of directories
# in real life, use Get-ChildItem -Directory
$DirList = @(
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderA_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder1SubFolderB_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_1'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_2'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderA_4'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_11'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_22'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_3'
[System.IO.DirectoryInfo]'C:ParentFolderChildFolder2SubFolderB_44'
)

$GroupedDirList = $DirList |
# changed from sorting by the FullName to sorting by the trailing number
# thanks to LotPings for pointing out the glitch with multi-digit numbers
Sort-Object {[int]$_.FullName.Split('_')[1]} |
Group-Object {$_.FullName.Split('_')[0]}

foreach ($GDL_Item in $GroupedDirList)
{
$GDL_Item.Group |
Select-Object -SkipLast 1 |
ForEach-Object {
# remove the quotes, the Write-Host, and the "$()" when you do this for real
# can't use the "-WhatIf" parameter here since the dirs don't actually exist on my system
Write-Host "Remove-Item -LiteralPath $($_.FullName) -Recurse -WhatIf"
}

'=' * 20
}


output ...



Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderA_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder1SubFolderB_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_1 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_2 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderA_3 -Recurse -WhatIf
====================
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_3 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_11 -Recurse -WhatIf
Remove-Item -LiteralPath C:ParentFolderChildFolder2SubFolderB_22 -Recurse -WhatIf
====================






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 14:21

























answered Jan 2 at 14:03









Lee_DaileyLee_Dailey

2,4861811




2,4861811








  • 1





    Nice one(+1) was about to post a similar script. But what happens if the trailing count exceeds 9/one place ?

    – LotPings
    Jan 2 at 14:08













  • @LotPings - hah! very good point ... i forgot that Sort-Object would be doing a string sort without any awareness of numbers. [blush] i'm off to fix that ...

    – Lee_Dailey
    Jan 2 at 14:19






  • 1





    Many thanks Lee_Dailey. I have also tried your script and this works perfectly with the addition of "-recurse -force -confirm:$false". to remove Item. Thanks again.

    – Dustree
    Jan 2 at 17:17













  • @Dustree - you are most welcome! glad to have helped a little ... [grin]

    – Lee_Dailey
    Jan 2 at 17:44














  • 1





    Nice one(+1) was about to post a similar script. But what happens if the trailing count exceeds 9/one place ?

    – LotPings
    Jan 2 at 14:08













  • @LotPings - hah! very good point ... i forgot that Sort-Object would be doing a string sort without any awareness of numbers. [blush] i'm off to fix that ...

    – Lee_Dailey
    Jan 2 at 14:19






  • 1





    Many thanks Lee_Dailey. I have also tried your script and this works perfectly with the addition of "-recurse -force -confirm:$false". to remove Item. Thanks again.

    – Dustree
    Jan 2 at 17:17













  • @Dustree - you are most welcome! glad to have helped a little ... [grin]

    – Lee_Dailey
    Jan 2 at 17:44








1




1





Nice one(+1) was about to post a similar script. But what happens if the trailing count exceeds 9/one place ?

– LotPings
Jan 2 at 14:08







Nice one(+1) was about to post a similar script. But what happens if the trailing count exceeds 9/one place ?

– LotPings
Jan 2 at 14:08















@LotPings - hah! very good point ... i forgot that Sort-Object would be doing a string sort without any awareness of numbers. [blush] i'm off to fix that ...

– Lee_Dailey
Jan 2 at 14:19





@LotPings - hah! very good point ... i forgot that Sort-Object would be doing a string sort without any awareness of numbers. [blush] i'm off to fix that ...

– Lee_Dailey
Jan 2 at 14:19




1




1





Many thanks Lee_Dailey. I have also tried your script and this works perfectly with the addition of "-recurse -force -confirm:$false". to remove Item. Thanks again.

– Dustree
Jan 2 at 17:17







Many thanks Lee_Dailey. I have also tried your script and this works perfectly with the addition of "-recurse -force -confirm:$false". to remove Item. Thanks again.

– Dustree
Jan 2 at 17:17















@Dustree - you are most welcome! glad to have helped a little ... [grin]

– Lee_Dailey
Jan 2 at 17:44





@Dustree - you are most welcome! glad to have helped a little ... [grin]

– Lee_Dailey
Jan 2 at 17:44


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54006850%2fdeleting-folders-that-have-serialized-names-in-powershell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]