Solved

Help with move-item in a loop in Powershell

Posted on 2014-02-12
1
1,220 Views
Last Modified: 2014-02-28
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
0
Comment
Question by:ndalmolin_13
1 Comment
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39853594
You've used $file inside the loop - but that var isn't defined / changed.
You don't need to provide a file name, btw, just stuff in the filesystem object you got:
$logcount = ($Logs = get-childitem -path \\Server1\c$\logs).count

Foreach ($log in $logs)
{
  $counter++
  move-item $log -destination \\netshare\archive
  Write-progress -Activity 'Moving' -Status 'Moving' -PercentComplete (++$counter / $logcount * 100)
}

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Set OWA language and time zone in Exchange for individuals, all users or per database.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question