139

私はこのワンライナーを持っています:

get-WmiObject win32_logicaldisk -Computername remotecomputer

出力は次のとおりです。

DeviceID     : A:
DriveType    : 2
ProviderName :
FreeSpace    :
Size         :
VolumeName   :

DeviceID     : C:
DriveType    : 3
ProviderName :
FreeSpace    : 20116508672
Size         : 42842714112
VolumeName   :

DeviceID     : D:
DriveType    : 5
ProviderName :
FreeSpace    :
Size         :
VolumeName   :

どうすれば取得できFreespaceますSizeDeviceID C:?他の情報なしで、これら2つの値だけを抽出する必要があります。Selectコマンドレットで試しましたが、効果がありません。

編集: 数値のみを抽出して変数に保存する必要があります。

4

16 に答える 16

156

はるかに簡単な解決策:

Get-PSDrive C | Select-Object Used,Free

およびリモートコンピュータの場合(必要Powershell Remoting

Invoke-Command -ComputerName SRV2 {Get-PSDrive C} | Select-Object PSComputerName,Used,Free
于 2015-05-01T17:48:10.883 に答える
152
$disk = Get-WmiObject Win32_LogicalDisk -ComputerName remotecomputer -Filter "DeviceID='C:'" |
Select-Object Size,FreeSpace

$disk.Size
$disk.FreeSpace

値のみを抽出して変数に割り当てるには:

$disk = Get-WmiObject Win32_LogicalDisk -ComputerName remotecomputer -Filter "DeviceID='C:'" |
Foreach-Object {$_.Size,$_.FreeSpace}
于 2012-08-28T12:42:46.193 に答える
67

シンプルでクリーンなコマンドは1つだけですが、これはローカルディスクでのみ機能します

Get-PSDrive

ここに画像の説明を入力してください

Enter-PSSession -Computername ServerNameを実行してリモートサーバーでこのコマンドを使用し、Get-PSDriveを実行すると、サーバーから実行した場合と同じようにデータがプルされます。

于 2018-02-23T18:41:13.340 に答える
19

しばらく前に、複数のコンピューターにクエリを実行できるPowerShellの高度な関数(スクリプトコマンドレット)を作成しました。

関数のコードは100行強なので、ここで見つけることができます:PowerShellバージョンのdfコマンド

例については、使用法のセクションを確認してください。次の使用例は、一連のリモートコンピューター(PowerShellパイプラインからの入力)を照会し、人間が読める形式の数値を含むテーブル形式で出力を表示します。

PS> $cred = Get-Credential -Credential 'example\administrator'
PS> 'db01','dc01','sp01' | Get-DiskFree -Credential $cred -Format | Format-Table -GroupBy Name -AutoSize

   Name: DB01

Name Vol Size  Used  Avail Use% FS   Type
---- --- ----  ----  ----- ---- --   ----
DB01 C:  39.9G 15.6G 24.3G   39 NTFS Local Fixed Disk
DB01 D:  4.1G  4.1G  0B     100 CDFS CD-ROM Disc


   Name: DC01

Name Vol Size  Used  Avail Use% FS   Type
---- --- ----  ----  ----- ---- --   ----
DC01 C:  39.9G 16.9G 23G     42 NTFS Local Fixed Disk
DC01 D:  3.3G  3.3G  0B     100 CDFS CD-ROM Disc
DC01 Z:  59.7G 16.3G 43.4G   27 NTFS Network Connection


   Name: SP01

Name Vol Size   Used   Avail Use% FS   Type
---- --- ----   ----   ----- ---- --   ----
SP01 C:  39.9G  20G    19.9G   50 NTFS Local Fixed Disk
SP01 D:  722.8M 722.8M 0B     100 UDF  CD-ROM Disc
于 2013-06-15T19:11:36.127 に答える
11

別の方法は、文字列をWMIオブジェクトにキャストすることです。

$size = ([wmi]"\\remotecomputer\root\cimv2:Win32_logicalDisk.DeviceID='c:'").Size
$free = ([wmi]"\\remotecomputer\root\cimv2:Win32_logicalDisk.DeviceID='c:'").FreeSpace

また、異なる単位が必要な場合は、結果を1GBまたは1MBで割ることができます。

$disk = ([wmi]"\\remotecomputer\root\cimv2:Win32_logicalDisk.DeviceID='c:'")
"Remotecomputer C: has {0:#.0} GB free of {1:#.0} GB Total" -f ($disk.FreeSpace/1GB),($disk.Size/1GB) | write-output

出力は次のとおりです。Remotecomputer C: has 252.7 GB free of 298.0 GB Total

于 2012-09-17T21:19:03.427 に答える
11

他の提案で遭遇した2つの問題があります

    1)タスクスケジューラでPowerShellを実行する場合、ドライブマッピングはサポートされません
    2)リモートコンピュータで「get-WmiObject」を使用しようとすると、 Access isdeniedエラーエラーが発生する場合があります(もちろん、インフラストラクチャの設定によって異なります)。

これらの問題に悩まされない別の方法は、UNCパスでGetDiskFreeSpaceExを使用することです。

function getDiskSpaceInfoUNC($p_UNCpath, $p_unit = 1tb, $p_format = '{0:N1}')
{
    # unit, one of --> 1kb, 1mb, 1gb, 1tb, 1pb
    $l_typeDefinition = @' 
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
        [return: MarshalAs(UnmanagedType.Bool)] 
        public static extern bool GetDiskFreeSpaceEx(string lpDirectoryName, 
            out ulong lpFreeBytesAvailable, 
            out ulong lpTotalNumberOfBytes, 
            out ulong lpTotalNumberOfFreeBytes); 
'@
    $l_type = Add-Type -MemberDefinition $l_typeDefinition -Name Win32Utils -Namespace GetDiskFreeSpaceEx -PassThru

    $freeBytesAvailable     = New-Object System.UInt64 # differs from totalNumberOfFreeBytes when per-user disk quotas are in place
    $totalNumberOfBytes     = New-Object System.UInt64
    $totalNumberOfFreeBytes = New-Object System.UInt64

    $l_result = $l_type::GetDiskFreeSpaceEx($p_UNCpath,([ref]$freeBytesAvailable),([ref]$totalNumberOfBytes),([ref]$totalNumberOfFreeBytes)) 

    $totalBytes     = if($l_result) { $totalNumberOfBytes    /$p_unit } else { '' }
    $totalFreeBytes = if($l_result) { $totalNumberOfFreeBytes/$p_unit } else { '' }

    New-Object PSObject -Property @{
        Success   = $l_result
        Path      = $p_UNCpath
        Total     = $p_format -f $totalBytes
        Free      = $p_format -f $totalFreeBytes
    } 
}
于 2016-02-02T16:24:47.610 に答える
7

コマンドライン:

powershell gwmi Win32_LogicalDisk -ComputerName remotecomputer -Filter "DriveType=3" ^|
select Name, FileSystem,FreeSpace,BlockSize,Size ^| % {$_.BlockSize=
(($_.FreeSpace)/($_.Size))*100;$_.FreeSpace=($_.FreeSpace/1GB);$_.Size=($_.Size/1GB);$_}
^| Format-Table Name, @{n='FS';e={$_.FileSystem}},@{n='Free, Gb';e={'{0:N2}'-f
$_.FreeSpace}}, @{n='Free,%';e={'{0:N2}'-f $_.BlockSize}},@{n='Capacity ,Gb';e={'{0:N3}'
-f $_.Size}} -AutoSize

出力:

Name FS   Free, Gb Free,% Capacity ,Gb

---- --   -------- ------ ------------

C:   NTFS 16,64    3,57   465,752

D:   NTFS 43,63    9,37   465,759

I:   NTFS 437,59   94,02  465,418

N:   NTFS 5,59     0,40   1 397,263

O:   NTFS 8,55     0,96   886,453

P:   NTFS 5,72     0,59   976,562

コマンドライン:

wmic logicaldisk where DriveType="3" get caption, VolumeName, VolumeSerialNumber, Size, FileSystem, FreeSpace

アウト:

Caption  FileSystem  FreeSpace     Size           VolumeName  VolumeSerialNumber

C:       NTFS        17864343552   500096991232   S01         EC641C36

D:       NTFS        46842589184   500104687616   VM1         CAF2C258

I:       NTFS        469853536256  499738734592   V8          6267CDCC

N:       NTFS        5998840832    1500299264512  Vm-1500     003169D1

O:       NTFS        9182349312    951821143552   DT01        A8FC194C

P:       NTFS        6147043840    1048575144448  DT02        B80A0F40

コマンドライン:

wmic logicaldisk where Caption="C:" get caption, VolumeName, VolumeSerialNumber, Size, FileSystem, FreeSpace

アウト:

Caption  FileSystem  FreeSpace    Size          VolumeName  VolumeSerialNumber

C:       NTFS        17864327168  500096991232  S01         EC641C36

command-line:

dir C:\ /A:DS | find "free"

out:
               4 Dir(s)  17 864 318 976 bytes free

dir C:\ /A:DS /-C | find "free"

out:
               4 Dir(s)     17864318976 bytes free
于 2012-12-28T02:10:19.540 に答える
7
Get-PSDrive C | Select-Object @{ E={$_.Used/1GB}; L='Used' }, @{ E={$_.Free/1GB}; L='Free' }
于 2017-04-04T17:57:36.060 に答える
4

を返すGet-Volumeコマンドが見つかっSizeRemainingたので、のようなもの(Get-Volume -DriveLetter C).SizeRemaining / (1e+9)を使用して、ディスクCの残りのGbを確認できます。より高速に動作するようですGet-WmiObject Win32_LogicalDisk

于 2020-08-07T16:40:30.343 に答える
3
PS> Get-CimInstance -ComputerName bobPC win32_logicaldisk | where caption -eq "C:" | foreach-object {write " $($_.caption) $('{0:N2}' -f ($_.Size/1gb)) GB total, $('{0:N2}' -f ($_.FreeSpace/1gb)) GB free "}  
C: 117.99 GB total, 16.72 GB free 

PS> 
于 2015-05-07T17:49:38.417 に答える
2

ここからダウンロードできるpsExecツールを知っています

ツールパッケージからpsinfo.exeが提供されます。基本的な使用法は、powershell/cmdで次のようになります。

ここに画像の説明を入力してください

しかし、あなたはそれで多くのオプションを持つことができます

使用法:psinfo [[\ computer [、computer [、..] | @file [-u user [-p psswd]]] [-h] [-s] [-d] [-c [-t delimiter]] [filter]

\computer指定された1つまたは複数のリモートコンピューターでコマンドを実行します。コマンドがローカルシステムで実行されるコンピューター名を省略した場合、およびワイルドカード(\ *)を指定した場合、コマンドは現在のドメイン内のすべてのコンピューターで実行されます。

@file   Run the command on each computer listed in the text file specified.
-u  Specifies optional user name for login to remote computer.
-p  Specifies optional password for user name. If you omit this you will be prompted to enter a hidden password.
-h  Show list of installed hotfixes.
-s  Show list of installed applications.
-d  Show disk volume information.
-c  Print in CSV format.
-t  The default delimiter for the -c option is a comma, but can be overriden with the specified character.

filter Psinfoは、フィルターに一致するフィールドのデータのみを表示します。たとえば、「psinfo service」は、サービスパックフィールドのみを一覧表示します。

于 2016-10-04T05:46:24.853 に答える
1

PowerShellの場合:

"FreeSpace C:  " + [math]::Round((Get-Volume -DriveLetter C).SizeRemaining / 1Gb) + " GB"
于 2021-05-14T00:46:57.273 に答える
0

Enter-PSsession pcNameを使用してコンピューターにリモート接続し、Get-PSDrive と入力します

これにより、使用済みおよび残りのすべてのドライブとスペースが一覧表示されます。フォーマットされたすべての情報を表示する必要がある場合は、次のようにFLにパイプします。Get-PSdrive| FL *

于 2015-06-15T13:58:57.287 に答える
0

私は私を助けるためにこの単純な関数を作成しました。これにより、 Get-WmiObjectWhere-Objectステートメントなどをインライン化することで、呼び出しが非常に読みやすくなります。

function GetDiskSizeInfo($drive) {
    $diskReport = Get-WmiObject Win32_logicaldisk
    $drive = $diskReport | Where-Object { $_.DeviceID -eq $drive}

    $result = @{
        Size = $drive.Size
        FreeSpace = $drive.Freespace
    }
    return $result
}

$diskspace = GetDiskSizeInfo "C:"
write-host $diskspace.FreeSpace " " $diskspace.Size
于 2016-03-01T14:24:40.617 に答える
0

複数のドライブ文字を確認したり、ローカルドライブとネットワークドライブ間でフィルターをかけたりする場合は、PowerShellを使用してWin32_LogicalDiskWMIクラスを利用できます。簡単な例を次に示します。

$localVolumes = Get-WMIObject win32_volume;

foreach ($vol in $localVolumes) {
  if ($vol.DriveLetter -ne $null ) {
    $d = $vol.DriveLetter[0];
    if ($vol.DriveType -eq 3) {
      Write-Host ("Drive " + $d + " is a Local Drive");
    }
    elseif ($vol.DriveType -eq 4) {
      Write-Host ("Drive" + $d + " is a Network Drive");
    }
    else {
      // ... and so on
    }

    $drive = Get-PSDrive $d;
    Write-Host ("Used space on drive " + $d + ": " + $drive.Used + " bytes. `r`n");
    Write-Host ("Free space on drive " + $d + ": " + $drive.Free + " bytes. `r`n");
  }
}

上記の手法を使用して、すべてのドライブをチェックし、ユーザー定義の割り当てを下回るたびに電子メールアラートを送信するPowershellスクリプトを作成しました。あなたは私のブログのこの投稿からそれを得ることができます。

于 2017-08-31T14:19:44.483 に答える
0

PowerShellの楽しみ

Get-WmiObject win32_logicaldisk -Computername <ServerName> -Credential $(get-credential) | Select DeviceID,VolumeName,FreeSpace,Size | where {$_.DeviceID -eq "C:"}
于 2019-11-05T22:41:39.427 に答える