Tuesday, September 4, 2018

while and Foreach in PowerShell!

This script will move all of the .bak files, and just the .bak files from the immediate folder up one folder. Note the -ne for not equals and the -lt for less than! Cool stuff.

$a = Get-Location
$e = $a.Path
$i = $e.length
$o = 0
$u = ""
$y = @()
while($e[$i-1] -ne "\") { $i-- }
while($o -lt $i-1) { $u = $u + $e[$o] ; $o++ }
$y += Get-ChildItem -Filter *.bak
Foreach ($x in $y) { Move-Item -Path ($e + "/" + $x.Name) -Destination $u }
echo "To quote Annette Hanshaw: That's all"

No comments:

Post a Comment