Home > Net >  PowerShell - Print Move-Item in Console
PowerShell - Print Move-Item in Console

Time:12-02

I have the following code:

        $SchoolFolder = "C:\Users\MyUser\Desktop\School Folder\$StudentName\$Month. $MonthWrite\$Day. $DayWrite"
        $MP4Lenght = (Get-ChildItem -Path $RenderFolder).Length -ne "0"
        $MP4existsToCopy = Test-Path -Path "$RenderFolder\*.mp4"
        If (($MP4existsToCopy -eq $True) -and ($MP4Lenght -eq $True)) {
        Get-ChildItem $MyFolder | 
                Where-Object { $_.Length -gt 0KB} |
                Move-Item -Destination (new-item -type directory -force ($SchoolFolder   $newSub)) -force -ea 0
        Write-Host "Done!"
        }

I would like to know how do I make all correspondence in $MP4Lenght be printed in the console with the format $MP4Lenght "was moved", because that way I can know which files were moved.

CodePudding user response:

Why not just use -verbose?

Move-Item -Destination (new-item -type directory -force ($SchoolFolder   $newSub)) -force -ea 0 -Verbose

CodePudding user response:

Your "exist to copy" logic isn't really required, because if the file doesn't exist then get-childitem is not going to find it. Similarly with the check if MP4lenght is true.

The following will check if the file does not exist in the source and does exist in the destination and if that is true then write to the host that the file has moved:

$source = 'C:\Users\myuser\playground\powershell\Source\'
$destination = 'C:\Users\myuser\playground\powershell\Destination'
$files = Get-ChildItem $source -File | where-object {$_.Length -ne 0}

foreach ($file in $files) {

Move-Item $file.FullName -Destination .\Destination

if (-not(Test-Path $file.FullName) -and (test-path (Join-Path -Path $destination -ChildPath $file.Name))) {
    Write-Host "$($file.name) has moved"
}

}

  • Related