Link to home
Start Free TrialLog in
Avatar of ndalmolin_13
ndalmolin_13Flag for United States of America

asked on

Help with move-item in a loop in Powershell

Hello All,

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)
{
$counter++
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.

Nick
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial