Solved

Help with move-item in a loop in Powershell

Posted on 2014-02-12
1
1,132 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 68

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now