I'm facing a problem which the result of getdrive is not accurate. Clueless.
As seen in the screenshots below, 10.1.105.203 has a total space of 7.81TB, but from what powershell gives me, it is only roughly 4TB. Pretty confusing here.
You can also use Get-Psdrive to get the size of a drive.
PS C:\> Get-PSDrive -PSProvider filesystem
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
C 48.92 195.13 FileSystem C:\
D 30.82 69.18 FileSystem D:\
E .12 121.49 FileSystem E:\
You can always do a little manipulation to achieve multiple things with this, e.g,
PS C:\> Get-PSDrive -PSProvider filesystem | select Name, #{n= 'Used(GB)' ; e = {"{0:N2}" -f ($_.used/1GB)}}, #{n= 'Free
(GB)' ; e = {"{0:N2}" -f ($_.Free/1GB)}}, #{n= 'Total(GB)' ; e = {"{0:N2}" -f (($_.used + $_.Free)/1GB)}} | ft -auto
Name Used(GB) Free(GB) Total(GB)
---- -------- -------- ---------
C 48.92 195.13 244.04
D 30.82 69.18 100.00
E 0.12 121.49 121.62
You're not really using PowerShell. You're using the Windows Scripting Host's object model, the precursor to PowerShell, from PowerShell. Try using either:
C:\PS> Get-Volume
DriveLetter FileSystemLabel FileSystem DriveType HealthStatus SizeRemaining Size
----------- --------------- ---------- --------- ------------ ------------- ----
C NTFS Fixed Healthy 94.34 GB 237.96 GB
Recovery NTFS Fixed Healthy 10.19 MB 300 MB
or
C:\PS> Get-WmiObject Win32_Volume | Format-Table DriveLetter,Capacity,FreeSpace
DriveLetter Capacity FreeSpace
----------- -------- ---------
C: 255505461248 101292863488
314568704 10682368
Full agree with #Keith Hill, with the PowerShell way to get the result. The strange behaviour is that for your drive L:, I can see that the total size is ok for the 10Tb drive, so I would not be surprise if you receive the same results from the new commands.
The next thing, I would check is if quotas are activated on your server.
You can also use :
Get-WmiObject Win32_logicaldisk -filter "driveType=4"
Related
I'm trying to sort my drives using Powershell by free space. The purpose of this utility is to re-distribute one disk that is almost full across all the disks that are not as full.
Currently, my powershell script looks like this:
# Retrieves drives from My Computer
$Drives = gdr -PSProvider FileSystem
# Iterates over all drives found
foreach($Drive in $Drives) {
$Drive | Sort-Object -Property Free
}
The output is this:
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
C 151.09 688.31 FileSystem C:\
D 0.05 15.67 FileSystem D:\
F 0.09 2.96 FileSystem F:\
G 0.05 16.43 FileSystem G:\
H 0.03 9.73 FileSystem H:\
I 2.42 65.94 FileSystem I:\
But I'm expecting something like the Free (GB) Column to be sorted ASC
Any clues?
The problem was that I sorting the individual drive and not the collection as pointed out by #johnrsharpe and #Lee_Dailey
This ended up solving my problem:
# Iterates over all drives found
$SortedDrives = $Drives | Sort-Object -Property Used -Descending
foreach ($Drive in $SortedDrives) {
Write-Output $Drive.Name
}
Is there any cmdlet way to get if a Disk is fixed or removable given a code like this?
$disk = Get-Disk -Number 1
Get-DiskDriveType $disk
Where Get-DiskDriveType should return either Removable or Fixed.
Inventory Drive Types by Using PowerShell
https://blogs.technet.microsoft.com/heyscriptingguy/2014/09/10/inventory-drive-types-by-using-powershell
Two methods:
Get-Volume
DriveLetter FileSystemLabel FileSystem DriveType HealthStatus SizeRemaining Size
----------- ----------- ---------- --------- ---------- ---------- ----
C SSD NTFS Fixed Healthy 75.38 GB 148.53 GB
E HybridTe... NTFS Fixed Healthy 560.71 GB 931.39 GB
D FourTB_B... NTFS Fixed Healthy 1.5 TB 3.64 TB
F TwoTB_BU... NTFS Fixed Healthy 204.34 GB 1.82 TB
G USB3 NTFS Removable Healthy 6.73 GB 58.89 GB
Recovery NTFS Fixed Healthy 22.96 MB 300 MB
H CD-ROM Healthy 0 B 0 B
Or
$hash = #{
2 = "Removable disk"
3 = "Fixed local disk"
4 = "Network disk"
5 = "Compact disk"
}
Get-CimInstance Win32_LogicalDisk |
Select DeviceID, VolumeName,
#{LABEL='TypeDrive';EXPRESSION={$hash.item([int]$_.DriveType)}}
Get-Volume | Where-Object {$_.DriveType -eq 'removable'} | Get-Partition | Get-Disk | Where-Object {$_.Number -eq $diskNumber}
I want to use the Where-Object to limit the Output of Get-PSDrive to only network shares.
Get-PSDrive shows me the following:
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
A FileSystem A:\
Alias Alias
C 16.19 43.47 FileSystem C:\ Users\HansB\Documents
Cert Certificate \
D FileSystem D:\
Env Environment
Function Function
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
V 451.39 159.76 FileSystem \\192.168.71.31\fs_log_target
Variable Variable
W 197.72 FileSystem \\192.168.71.32\perf200
WSMan WSMan
X 197.72 FileSystem \\192.168.71.32\perf100
Y 271.52 34.33 FileSystem \\192.168.71.30\group200
Z 271.52 34.33 FileSystem \\192.168.71.30\group100
Then I want to get the \\192.168.71.30\group100 Network Share:
Get-PSDrive | Where-Object { $_.Root -match "\\\\192.168.71.30\\group100" }
But I get nothing, why does -match not work?
Use DisplayRoot instead of Root property
Get-PSDrive | Where-Object { $_.DisplayRoot -match "\\\\192.168.71.30\\group100" }
try to run
Get-PSDrive | Where-Object { $_.DisplayRoot -match "\\\\192.168.71.30\\group100" } | select *
Root will be your mapped drive letter, and DisplayRoot your UNC Path
EDIT: as a side note. For escaping regex use [regex]::Escape() method.
PS > [regex]::Escape("\\192.168.71.30\group100")
\\\\192\.168\.71\.30\\group100
Try using -eq instead:
Get-PSDrive | Where-Object {$_.Root -eq "\\192.168.71.30\group100"}
If yoy type Get-PSDrive | select root you will notice that for network drives it will print nothing. So there is something strange with network share roots.
Get-PSDrive | select root | %{write-host $_.Root.GetType()} will show that it's a system string, so not quite sure why it seems to be empty for network drives.
I'm trying to create a script which takes a folderpath as an argument and displays how full the partition with the folderpath is (0-100%).
In Linux I know of the df -T $directory command, but I cannot find any way of doing this in powershell. Any tips?
You can approximate the output of df -T by using the following Get-CIMINstance call, which queries WMI/CIM for the information you're requesting. As I'm not a linux expert, I'm not certain what specifically makes df -t a special command. If this output doesn't capture what you need, can you provide some more info?
Get-CimInstance Win32_logicaldisk | Select-Object SystemName,#{Name=‘Drive Letter‘;Expression={$_.DeviceID}},
#{Name=‘Drive Label’;Expression={$_.VolumeName}},`
#{Name=‘Size(MB)’;Expression={[int]($_.Size / 1MB)}},`
#{Name=‘FreeSpace%’;Expression={[math]::Round($_.FreeSpace / $_.Size,2)*100}} | ft
> SystemName Drive Letter Drive Label Size(MB) FreeSpace%
---------- ------------ ----------- -------- ----------
BEHEMOTH B: Aug 01 2014 172 0
BEHEMOTH C: 171353 10
BEHEMOTH D: Media 953867 20
BEHEMOTH E: Big Betty 2861459 88
BEHEMOTH H: VM 117726 37
BEHEMOTH T: 250 94
UPDATED
Ok, I created a function called df using this code below. You can provide a directory with -t to get the statistics for that particular partition. Otherwise, if run without a param it will give us the disk statistics for the current disk.
Function df {
Param
(
# Param1 help description
[Parameter(ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[Alias("t")]
$directory = (get-item (get-location)))
if ($PSBoundParameters.Count -ne 0){$directory = dget-item $directory}
$root = $directory.Root.Name -replace '\\','%'
Get-CimInstance Win32_logicaldisk -Filter ("DeviceID like '$root%'")| Select-Object SystemName,#{Name=‘Drive Letter‘;Expression={$_.DeviceID}},
#{Name=‘Drive Label’;Expression={$_.VolumeName}},`
#{Name=‘Size(MB)’;Expression={"{0:N0}" -f [int]($_.Size / 1MB)}},`
#{Name=‘FreeSpace%’;Expression={[math]::Round($_.FreeSpace / $_.Size,2)*100}} | ft
}
df -t .\SampleDir
>SystemName Drive Letter Drive Label Size(MB) FreeSpace%
---------- ------------ ----------- -------- ----------
DELLBOOK C: OSDisk 243,247 39
I am half way down with my work and now stuck.
I am trying to fetch information about available storage devices for a cluster.
I am able to fetch the list of available storage devices but unable to retrieve the physical disk, available free space, etc of these available storage.
I want like this. Is there any command to fetch physical disk name from Cluster Disk Name or directly can I get the disk details.
For Shared Disk I am able to retrieve the details (Get-ClusterSharedVolume) but not for a non-shared disk.
I want powershell or WMI script for doing so.
You can get this information from WMI, but it takes a couple steps:
$resources = Get-WmiObject -namespace root\MSCluster MSCluster_Resource -filter "Type='Physical Disk'"
$resources | foreach {
$res = $_
$disks = $res.GetRelated("MSCluster_Disk")
$disks | foreach {
$_.GetRelated("MSCluster_DiskPartition") |
select #{N="Name"; E={$res.Name}}, #{N="Status"; E={$res.State}}, Path, VolumeLabel, TotalSize, FreeSpace
}
} | ft
That will give you output like the following:
Name Status Path VolumeLabel TotalSize FreeSpace
---- ------ ---- ----------- --------- ---------
Cluster Disk 2 2 K: New Volume 5220 5163
SQL - FAS3070 SiteB 2 S: MC_SQL 5597 5455
SM Test 2 M: SM Test 1024 992
DTC - FAS3070B 2 F: MC_WITNESS 5346 5289
Cluster Disk Witness 2 E: New Volume 5322 5267
Cluster Disk 1 2 G: MC_DTC 5088 5035
Cluster Disk 3 2 T: SQL 5119 4999
If you don't care about the resource name/status you can skip those steps and jump straight to the partition (and it'll run much quicker):
gwmi -namespace root\MSCluster MSCluster_DiskPartition | ft Path, VolumeLabel, TotalSize, FreeSpace
Edit: Note that the size is in MB and a Status of "2" means that the disk is online.
you can use wmi like this:
Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" | Select DeviceID, FreeSpace
throw in a computername parameter if you wish to do it remotely
HTH,
Matt
PS. for a more readable report you can try this:
Get-WMIObject Win32_LogicalDisk -filter "DriveType=3" |
Select DeviceID, #{Name = "Free Space (%)" ; Expression= {[int] ($_.FreeSpace / $_.Size* 100)}},#{Name = "Free Space (GB)"; Expression = {[int]($_.Freespace / 1GB)}}, #{Name = "Size (GB)"; Expression = {[int]($_.Freespace / 1GB)}}