Übungen Drives

Diverse kleinere PowerShell-Codes zur weiteren Einstimmung.

temp Ordner aufräumen - altes Zeug (hier: 30 Tage+) entfernen:

 1$cutoff = (Get-Date) - (New-TimeSpan -Days 30)
 2$before = (Get-ChildItem $env:temp | Measure-Object Length -Sum).Sum
 3Get-ChildItem $env:temp | Where-Object { $_.Length -ne $null } |
 4    Where-Object { $_.LastWriteTime -lt $cutoff } |
 5    Remove-Item -Force -ErrorAction SilentlyContinue -Recurse -WhatIf
 6# simulation only, no files and folders will be deleted
 7# replace -WhatIf with -Confirm to confirm each delete
 8# remove -WhatIf altogether to delete without confirmation (at your own risk)
 9$after = (Get-ChildItem $env:temp | Measure-Object Length -Sum).Sum
10$freed = $before - $after
11"Cleanup freed {0:0.0} MB." -f ($freed/1MB)

Größe eines Ordners ermitteln: (hier Download)

1$folder = "$env:userprofile\Downloads"
2Get-ChildItem -Path $folder -Recurse -Force -ea 0 | Measure-Object -Property Length -Sum | ForEach-Object {
3    $sum = $_.Sum / 1MB
4        "Der Download-Ordner enthaelt derzeit {0:#,##0.0} MB storage." -f $sum
5        }

Größen Min vs. Max

1Get-ChildItem $env:windir |
2    Measure-Object -Property Length -Minimum -Maximum |
3    Select-Object -Property Minimum,Maximum

Speicherzeit alt vs. neu

1Get-ChildItem $env:windir |
2    Measure-Object -Property LastWriteTime -Minimum -Maximum |
3    Select-Object -Property Minimum,Maximum

Dateien umbenennen - Anm.: die Win8/10 Screenshots (Shortcut: Win+Druck) haben Leerzeichen!

1$global:i = 1
2$path = "$env:userprofile\Pictures\Screenshots"
3Get-ChildItem -Path $path -Filter *.png |
4    Rename-Item -NewName { "screnshot_$i.png"; $global:i++}

1GB Datei in Sekundenbruchteil

1$path = "$env:temp\testfile.txt"
2$file = [io.file]::Create($path)
3$file.SetLength(1gb)
4$file.Close()
5Get-Item $path

Alle Code-Snippets - wie immer ;-) - ohne Gewähr!