Help with move-item in a loop in Powershell
Posted on 2014-02-12
I want to automate the moving of log files from a server (named: Server1) to a network location, but I'm having issues using the move-item commandlet. If I do something like the following code, it work fine:
get-childitem -path \\Server1\c$\logs | move-item -destination \\netshare\archive
Moving the logs can take some time, so I wanted to add a progress bar. After some research, I'm trying to do the following
$logcount = ($Logs = get-childitem -path \\Server1\c$\logs).count
Foreach ($log in $logs)
move-item -literalpath $file.name -destination \\netshare\archive
Write-progress -Activity 'Moving' -Status 'Moving' -PercentComplete (($counter / $logcount)*100)
When I execute the code above, I get an error stating move-item cannot move item because the file does not exist. The files do exist. I can't figure out why I'm getting this message.
Also, does the write-progress portion look correct?
Thanks in advance for your help.