Script to Move Files

Welcome Forums General PowerShell Q&A Script to Move Files

This topic contains 3 replies, has 2 voices, and was last updated by

3 years, 10 months ago.

  • Author
  • #24361

    Points: 0
    Rank: Member

    Here's what I've got and am up against.

    I have an old 2003 server where I need to go into a few different directories and subdirectories, e.g., d:\share, d:\share2, and MOVE and file that HAS NOT BEEN ACCESSED in 18 months to an external hard drive, leaving the files that HAVE BEEN ACCESSED in the last 18 months in their current places.

    I'm not very good in PS, but here's what I've got:

    $newRoot = "c:\test"
    $baseRoot = "D:\ESSHARE\EmployeeShares\warmde00"
    $subCut = [int]$baseRoot.Length
    Get-ChildItem "$baseRoot" -Recurse { ! $_.PSIsContainer } |
    ForEach-Object {
        If ( $_.LastAccessTime -ge "10/16/2013" ) {
            try {
                [string]$baseDir = $_.Directory.FullName.substring($subCut)
            catch { }
            If ( ! (Test-Path $newRoot$baseDir) ) {
                mkdir "$newRoot$baseDir" -Force
            Move-Item $_.FullName -destination $newRoot$baseDir'\'$_
            if ( ( Get-ChildItem $_.Directory.FullName | Measure-Object ).Count -eq 0 ) {
                Remove-Item $_.Directory.FullName
        Else { Out-File -append C:\temp\NotMoved.txt -InputObject $_.FullName }

    Here's the other snag. The '03 server has PS1. I am trying to run this script from a domain computer with PS3 using Enter-PSSession -ComputerName -Credential .

    I'm really at a loss as this is not working. I get the following error:

    Get-ChildItem : Cannot find path 'D:\ESSHARE\EmployeeShares\' because it does not exist.
    At C:\Users\z0036sjw\Desktop\powershell.ps1:5 char:1
    + Get-ChildItem "$baseRoot" -Recurse { ! $_.PSIsContainer } |
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (D:\ESSHARE\EmployeeShares\:String) [Get-ChildItem], ItemNotFoundException
        + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand


  • #24374

    Points: 1
    Rank: Member

    I'd consider using robocopy. It has a /minlad switch that might be useful for this.

  • #24462

    Points: 0
    Rank: Member

    Something like this?


  • #24483

    Points: 1
    Rank: Member

    No to /Mir, you don't want that.

    Minage and maxage are based off of the creation time, but you said accessed, so you need minlad instead

    I'd start testing with

    Robocopy source destination /e /mov /minlad:548 /L

    /e is recursive to search thru subdirectories. And /L is list only, so that you can verify it is getting the right files. After you verify remove /L

The topic ‘Script to Move Files’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort