I am using PowerShell 2.0 on a Windows SBS 2008 machine with the latest service packs. I have a two line script that finds all empty folders in a directory:
$a = Get-ChildItem E:\File_Server -recurse | Where-Object {$_.PSIsContainer -eq $True}
$a | Where-Object {$_.GetFiles().Count -eq 0} | [what now?!]
The second line finds all folders that are empty, however I’m stumped on the last pipe. Here’s what I’ve tried so far (keep in mind that the following were tried after the second pipe in the second line above):
$_.move(F:\path) Yes, I tried that. Yes, I’m a PowerShell noob. Of course, I received the error “Expressions are only allowed as the first element of a pipeline.”
move-item -destination F:\Path I receive the lamest error ever: “Move-Item : Source and destination path must have identical roots. Move will not work across volumes.” Seriously? What kind of asinine limitation is that?! Moving on…
copy-item -destination F:\empty_folders Apparently I can get around the limitation of move-item by using copy-item and then use remove-item. No such luck, however. I started the script with copy-item first. PowerShell didn’t throw any errors, but also didn’t do any thing else. It just sat there for the better part of an hour. No directories were moved.
Summary:
How do I take the list of empty folders that I have and, using PowerShell, move those empty directories elsewhere (across volumes!) deleting the originals after the move? Notice the caveat “using PowerShell” as I’ve already thought about adding RoboCopy to the mix but would like to keep it within PowerShell.
I was pretty frustrated with this as well, but like you said
copy-itemfollowed byremove-itemwill work:Note this is not just a limitation with
Move-Itemand Powershell but applicable to .NetSystem.IO.Directory.Moveand to be fair, the documentation says so:http://technet.microsoft.com/en-us/library/dd315310.aspx
PS: You got the
"Expressions are only allowed as the first element of a pipeline."error because the$_.move(F:\path)that you were trying must be inside a foreach-object like so –%{$_.move(F:\path)}