File e cartelle
I cmdlet per esplorare e gestire il filesystem.
Get-ChildItem (ls, dir)
Get-ChildItem
Get-ChildItem C:\Windows
Get-ChildItem -Force
Get-ChildItem -Recurse
Get-ChildItem -Recurse -Filter *.log
Get-ChildItem -Directory
Get-ChildItem -File
Get-ChildItem -Name
Get-ChildItem -Recurse -Depth 2
Get-ChildItem C:\ -Recurse -Include *.docx -ErrorAction SilentlyContinue
| Parametro | Significato |
|---|---|
-Force | Include file nascosti e di sistema |
-Recurse | Entra nelle sottocartelle |
-Filter | Filtro veloce (supportato dal provider) |
-Include / -Exclude | Filtri aggiuntivi (funzionano meglio con -Recurse) |
-Directory / -File | Solo cartelle / solo file |
-Depth N | Limita la profondità |
-Name | Solo i nomi |
-Filter è molto più veloce di -Include su grandi quantità di file.
Filtrare per dimensione e data
Get-ChildItem -Recurse -File | Where-Object Length -gt 100MB
Get-ChildItem -Recurse -File | Where-Object LastWriteTime -lt (Get-Date).AddDays(-30)
Get-ChildItem -Recurse -File | Sort-Object Length -Descending | Select-Object -First 10 FullName, @{n='MB';e={[math]::Round($_.Length/1MB,1)}}
Le unità KB, MB, GB sono riconosciute direttamente.
Spostarsi
Set-Location C:\Windows
Set-Location ..
Set-Location ~
Push-Location C:\Temp
Pop-Location
Get-Location
Creare
New-Item -ItemType Directory -Path C:\Progetti\Nuovo
New-Item -ItemType File -Path .\file.txt
New-Item -ItemType File -Path .\file.txt -Value "contenuto iniziale"
New-Item -ItemType SymbolicLink -Path .\link -Target C:\Origine
Crea anche le cartelle intermedie.
Copiare, spostare, rinominare
Copy-Item .\file.txt D:\Backup\
Copy-Item .\cartella D:\Backup\ -Recurse
Move-Item .\file.txt D:\Dati\
Rename-Item .\vecchio.txt nuovo.txt
Rinominare in massa:
Get-ChildItem *.txt | Rename-Item -NewName { $_.Name -replace '\.txt$', '.bak' }
Get-ChildItem *.jpg | Rename-Item -NewName { "foto_" + $_.Name }
Eliminare
Remove-Item .\file.txt
Remove-Item .\cartella -Recurse
Remove-Item .\cartella -Recurse -Force
Remove-Item *.tmp -WhatIf
L'eliminazione è definitiva (nessun Cestino). Prova sempre con -WhatIf.
Per spedire al Cestino:
Add-Type -AssemblyName Microsoft.VisualBasic
[Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile("C:\file.txt",'OnlyErrorDialogs','SendToRecycleBin')
Verificare l'esistenza
Test-Path C:\file.txt
Test-Path C:\Cartella -PathType Container
if (-not (Test-Path .\log)) { New-Item -ItemType Directory .\log }
Informazioni su un file
Get-Item .\file.txt | Format-List *
(Get-Item .\file.txt).Length
(Get-Item .\file.txt).LastWriteTime
Get-FileHash .\file.iso -Algorithm SHA256
Attributi
(Get-Item .\file.txt).Attributes
Set-ItemProperty .\file.txt -Name IsReadOnly -Value $true
(Get-Item .\file.txt -Force).Attributes += 'Hidden'
Percorsi
Join-Path C:\Dati sottocartella\file.txt
Split-Path C:\Dati\file.txt -Parent
Split-Path C:\Dati\file.txt -Leaf
Resolve-Path .\file.txt
$env:USERPROFILE
$PSScriptRoot
Archivi ZIP
Compress-Archive -Path .\cartella -DestinationPath .\archivio.zip
Compress-Archive -Path .\file1, .\file2 -DestinationPath .\archivio.zip -Update
Expand-Archive .\archivio.zip -DestinationPath .\estratto
Spazio occupato da una cartella
(Get-ChildItem C:\Dati -Recurse -File | Measure-Object Length -Sum).Sum / 1GB