Solved

Help with move-item in a loop in Powershell

Posted on 2014-02-12
1
1,311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A brief introduction to what I consider to be the best editor for PowerShell.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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