Elencare tutti i dispositivi, partizioni e volumi in PowerShell
-
12-09-2019 - |
Domanda
Non ho più volumi (come quasi tutti al giorno d'oggi): su Windows finiscono specificato come C :, D: e così via. Come faccio a elencare tutti questi, come su una macchina Unix con "ls / mnt /" con Powershell?
Soluzione
Per ottenere tutte le unità del file system, è possibile utilizzare il seguente comando:
gdr -PSProvider 'FileSystem'
gdr
è un alias Get-PSDrive
, che include tutte le "unità virtuali" per il registro di sistema, ecc.
Altri suggerimenti
Get-Volume
si otterrà: LetteraUnità, FileSystemLabel, FileSystem, DriveType, HealthStatus, SizeRemaining e dimensione
In Windows PowerShell:
Get-PSDrive
[System.IO.DriveInfo]::getdrives()
wmic diskdrive
wmic volume
Anche il dskwipe utility: http://smithii.com/dskwipe
dskwipe.exe -l
In primo luogo, su Unix si usa mount
, non ls /mnt
:. Molte cose non sono montati in /mnt
In ogni caso, c'è il comando DOS mountvol
, che continua a lavorare in PowerShell, e c'è il Get-PSDrive
PowerShell-specifici.
Questo è piuttosto vecchio, ma ho trovato seguendo la pena notare:
PS N:\> (measure-command {Get-WmiObject -Class Win32_LogicalDisk|select -property deviceid|%{$_.deviceid}|out-host}).totalmilliseconds
...
928.7403
PS N:\> (measure-command {gdr -psprovider 'filesystem'|%{$_.name}|out-host}).totalmilliseconds
...
169.474
Senza filtraggio proprietà, sul mio sistema di test, 4319.4196ms a 1777.7237ms. A meno che non ho bisogno di un oggetto PS-Drive è tornato, mi atterrò con WMI.
EDIT: Penso che abbiamo un vincitore: PS N:> (misura-comando {[System.IO.DriveInfo] :: getdrives () |% {$ _ nome} | Out-Host.}) Per talmilliseconds. 110.9819
Anche se questo non è 'PowerShell' specifica ... si può facilmente elencare le unità e le partizioni usando diskpart, list volume
PS C:\Dev> diskpart
Microsoft DiskPart version 6.1.7601
Copyright (C) 1999-2008 Microsoft Corporation.
On computer: Box
DISKPART> list volume
Volume ### Ltr Label Fs Type Size Status Info
---------- --- ----------- ----- ---------- ------- --------- --------
Volume 0 D DVD-ROM 0 B No Media
Volume 1 C = System NTFS Partition 100 MB Healthy System
Volume 2 G C = Box NTFS Partition 244 GB Healthy Boot
Volume 3 H D = Data NTFS Partition 687 GB Healthy
Volume 4 E System Rese NTFS Partition 100 MB Healthy
Abbiamo più volumi per unità (alcuni sono montati sulla sottodirectory sul disco). Questo codice mostra un elenco dei punti di montaggio e le etichette di volume. Ovviamente è possibile anche estrarre spazio libero e così via:
gwmi win32_volume|where-object {$_.filesystem -match "ntfs"}|sort {$_.name} |foreach-object {
echo "$(echo $_.name) [$(echo $_.label)]"
}
comando Esegui:
Get-PsDrive -PsProvider FileSystem
Per ulteriori informazioni si veda:
PS Funzione:> get-PSDrive