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."
m开发者_如何学编程ove-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-item
followed by remove-item
will work:
gci e:\file_server | ?{$_.PSIsContainer -eq $true} | ?{$_.GetFiles().count -eq 0} |
%{copy-item -LiteralPath $_.fullname -Destination f:\path; remove-item $_.fullname}
Note this is not just a limitation with Move-Item
and Powershell but applicable to .Net System.IO.Directory.Move
and to be fair, the documentation says so:
Move-Item will move files between drives that are supported by the same provider, but it will move directories only within the same drive.
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)}
精彩评论