Solved

Help with move-item in a loop in Powershell

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

Suggested Solutions

Title # Comments Views Activity
Need to combine two scripts 2 33
Powershell to query AD 3 35
Start-VM 3 18
Running powershell scripts from ASP.NET 6 29
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article will help you understand what HashTables are and how to use them in PowerShell.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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