November 20, 2014

Move files with Powershell (Try – Catch – Finally)

Filed under: Tips — donrsh @ 4:48 pm
$ErrorActionPreference = "stop"
$Source = "c:\share\*"
$Destination = "C:\Test"
$days = "-1"
    #$problem = $false
    Write-Host "Getting list of the files from last $days days..." -ForegroundColor Green
    $Total = Get-ChildItem -Path $Source | Measure-Object
    Write-Host "There is $($Total.count) objects in $Source" -ForegroundColor Green
    if (get-childitem -Path $Source |  where-object {$_.LastWriteTime -lt (get-date).AddDays($days)})
        Write-Host "Moving the files to $destination" -ForegroundColor Green
        move-item -Path $Source -destination $Destination -Verbose
       Write-Host "There is no files less than: $days" -ForegroundColor Red

    #$problem = $true
    Write-Host "An error has occurred...  Error details: $_." -ForegroundColor Red

        Write-Host "End" -ForegroundColor Green

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: