I am trying to get the free percentage like present in Disk Management
$Diskmgmt = Get-Volume | select DriveLetter,FileSystemLabel,FileSystem,DriveType,HealthStatus,OperationalStatus,SizeRemaining,Size
foreach($dsk in $Diskmgmt)
{
$dl = $dsk.DriveLetter
$fsl = $dsk.FileSystemLabel
$fs = $dsk.FileSystem
$dt = $dsk.DriveType
$hs = $dsk.HealthStatus
$os = $dsk.OperationalStatus
$sizer = [math]::round($dsk.SizeRemaining /1Gb, 2)
$siz = [math]::round($dsk.Size /1Gb, 2)
$PercentFree = [Math]::Round(($sizer / $siz) * 100, 2)
but the calculation coming like below
Capacity Free Space %Free
154.82 GB 200 GB 77 %
0 GB 0 GB 77 %
1.96 GB 6 GB 33 %
0.15 GB 0.49 GB 31 %
52.32 GB 99.51 GB 53 %
11.19 GB 11.23 GB 100 %
9.95 GB 10 GB 99 %
Please let me know if I am doing it correctly.
I'm guessing you're looking for something like this:
$props = #(
'DriveLetter'
'FileSystemLabel'
'FileSystem'
'DriveType'
'HealthStatus'
'OperationalStatus'
#{
Name = 'SizeRemaining'
Expression = { "{0:N3} Gb" -f ($_.SizeRemaining/ 1Gb) }
}
#{
Name = 'Size'
Expression = { "{0:N3} Gb" -f ($_.Size / 1Gb) }
}
#{
Name = '% Free'
Expression = { "{0:P}" -f ($_.SizeRemaining / $_.Size) }
}
)
Get-Volume -DriveLetter C, D | Select-Object $props | Format-Table
As an example this is how it looks on my laptop for Drives C and D:
DriveLetter FileSystemLabel FileSystem DriveType HealthStatus OperationalStatus SizeRemaining Size % Free
----------- --------------- ---------- --------- ------------ ----------------- ------------- ---- ------
D NTFS Fixed Healthy OK 748.731 Gb 931.512 Gb 80.38%
C NTFS Fixed Healthy OK 170.959 Gb 236.764 Gb 72.21%
Using get-psdrive and the 'p' format specifier:
get-psdrive c | % { $_.free/($_.used + $_.free) } | % tostring p
9.24%
Related
Below is the text file data I have
Disk ### Status Size Free Dyn Gpt
-------- ------------- ------- ------- --- ---
Disk 0 Online 100 GB 1024 KB
Disk 1 Online 6144 MB 1024 KB
Disk 2 Online 200 GB 1024 KB
Disk 3 Online 10 GB 1024 KB
I want to put this data in csv like below, removing the last 2 columns and adding the server value
server Disk ### Status Size Free
------ -------- ------------- ------- -------
s1 Disk 0 Online 100 GB 1024 KB
s1 Disk 1 Online 6144 MB 1024 KB
s1 Disk 2 Online 200 GB 1024 KB
s1 Disk 3 Online 10 GB 1024 KB
below is the code
$list = Get-Content -Path "C:\server.txt"
foreach($server in $list)
{
$diskpart = cmd /c 'echo list disk | diskpart'
}
$Lines = $diskpart
$Out = $False
$Line=#()
ForEach ($Line In $Lines)
{
If ($Line -match 'DISKPART>')
{
$Out = $False
}
If ($Out -eq $True)
{
$Line | Out-File "C:\d.txt" -Append
}
If ($Line -match 'DISKPART>')
{
$Out = $True
}
}
$data = Import-Csv -Path C:\d.txt
I tried reading line using foreach,but getting output with headers and ---
Disk ### Status Size Free Dyn Gpt
---------------------------------------------------
Disk 0 Online 100 GB 1024 KB
Please let me know how can I extract columns from text file and append the servername to it. Need some idea to do this
One way of converting this into a CSV file is like below.
Assuming you have the output from diskpart in a string array $lines and the current server is called 's1' :
$result = switch -Regex ($lines) {
'^\s*Disk \d' {
$disk,$status,$size,$free = $_.Trim() -split '\s{2,}'
[PsCustomObject]#{
'Server' = $server # 's1' in this example
'Disk ###' = $disk
'Status' = $status
'Size' = $size
'Free' = $free
}
}
}
# output on console screen
$result | Format-Table -AutoSize
# output to CSV file
$result | Export-Csv -Path 'C:\diskinfo.csv' -NoTypeInformation
Result on screen:
Server Disk ### Status Size Free
------ -------- ------ ---- ----
s1 Disk 0 Online 100 GB 1024 KB
s1 Disk 1 Online 6144 MB 1024 KB
s1 Disk 2 Online 200 GB 1024 KB
s1 Disk 3 Online 10 GB 1024 KB
P.S. It is strongly not recommended to use Out-File to build a CSV file. Use that only if other options are not available. To create CSV use Export-Csv
If you need to do this on a series of computers, you might use this:
$list = Get-Content -Path "C:\server.txt"
# parameter -ComputerName can handle an array of computer names at once
$lines = Invoke-Command -ComputerName $list -ScriptBlock { "list disk" | diskpart }
$result = switch -Regex ($lines) {
'^On computer: (\w+)' { $server = $Matches[1].Trim() }
'^\s*Disk \d' {
$disk,$status,$size,$free = $_.Trim() -split '\s{2,}'
[PsCustomObject]#{
'Server' = $server
'Disk ###' = $disk
'Status' = $status
'Size' = $size
'Free' = $free
}
}
}
# output on console screen
$result | Format-Table -AutoSize
# output to CSV file
$result | Export-Csv -Path 'C:\diskinfo.csv' -NoTypeInformation
I made this code to count disk capacity, but when i run it with only the SSD in my laptop i get 0GB. after I insert USB/external space it count the ssd + the USB.
$disk = Get-WmiObject Win32_DiskDrive
$capacity = 0;
for($i = 0;$i -lt $disk.Count; $i++){
$capacity = $capacity + [math]::round(($disk[$i].Size/1GB),2)
}
Write-Host $capacity "GB"
This works fine -> $disk.Size
Why does it not take the first [0] in my for loop?
I cannot answer why your for loop is failing without observing your environment, but there is almost never a use-case for it. You should instead opt for a foreach loop:
$capacity = foreach ($disk in Get-WmiObject -Class Win32_DiskDrive)
{
[Math]::Round(($disk.Size / 1GB), 2)
}
"$(($capacity | Measure-Object -Sum).Sum)GB"
Assuming Windows 10 since you didn't mention OS and the CMDLETS in Windows 10 are so much better. *See at the bottom for a Windows 7 version.
For disk information, I prefer using Get-PhysicalDisk, like this for example:
$DiskInfo = foreach ($disk in Get-PhysicalDisk) {
[string]$name = $disk.FriendlyName
[string]$type = $disk.MediaType
[int]$capacity = $disk.size / 1GB
[pscustomobject]#{
"Type"=$type;
"Name"=$name;
"Capacity (GB)"=$capacity;
}
}
$DiskInfo
In my environement where I have one SSD and one mechanical HDD, it will return:
Name Type Capacity (GB)
---- ---- -------------
SAMSUNG MZ7TY256HDHP-000L7 SSD 238
ST500LX025-1U717D HDD 466
If you wanted information for JUST the SSD for example, you could do this :
$DiskInfo = foreach ($disk in Get-PhysicalDisk | Where-Object {$_.MediaType -eq "SSD"} ) {
[string]$name = $disk.FriendlyName
[string]$type = $disk.MediaType
[int]$capacity = $disk.size / 1GB
[pscustomobject]#{
"Type"=$type;
"Name"=$name;
"Capacity (GB)"=$capacity;
}
}
$DiskInfo
Which returns only the SSD:
Type Name Capacity (GB)
---- ---- -------------
SSD SAMSUNG MZ7TY256HDHP-000L7 238
Explanation: Foreach disk connected, store name, media type and capacity in variables. *Divide byte capacity by 1GB to get a better number to look at.
Still in the Foreach, create a custom object at every iteration containing the 3 variables.
All put together, you can then output your variable DiskInfo which contains all the objects.
If on Windows 7, media type isn't available so you can't use it. Instead you can do:
$DiskInfo = foreach ($disk in Get-WmiObject -Class Win32_DiskDrive) {
[string]$name = $disk.model
[int]$capacity = $disk.size / 1GB
[pscustomobject]#{
"Name"=$name;
"Capacity (GB)"=$capacity;
}
}
$DiskInfo
Which will return something like:
Name Capacity (GB)
---- -------------
SAMSUNG MZ7TY256HDHP-000L7 238
ST500LX025-1U717D 466
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 make some changes to below script .
try{
$space = Get-WmiObject Win32_logicaldisk `
| Format-Table DeviceID,`
#{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}}, `
#{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}}, `
#{Name="Free (%)";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}} `
-AutoSize
}
catch
{
echo "Exception Occurred. Please try again on $servername"
}
echo "The total space on $servername are given below:"
echo $space
I want to add a column which shows used space without any changes to output format.
output is below:
The total space on are given below:
DeviceID Size(GB) Free Space(GB) Free (%)
-------- -------- -------------- --------
A: 0 0
C: 60 41 69 %
D: 100 78 78 %
E: 200 190 95 %
G: 0 0
You can extend the table as follows
try
{
$space = Get-WmiObject Win32_logicaldisk | Format-Table DeviceID,
#{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}},
#{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}},
#{Name="Free (%)";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}},
#{Name="Used Space(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb - $_.freespace/1gb))}},
#{Name="Used Space (%)";Expression={"{0,6:P0}" -f(($_.size/1gb - $_.freespace/1gb) / ($_.size/1gb))}}
-AutoSize
}
catch
{
echo "Exception Occurred. Please try again on $servername"
}
echo "The total space on $servername are given below:"
echo $space
I found the answer myself,refer to below code:
$servername = hostname
#checking for disk space
try{
$space = Get-WmiObject Win32_logicaldisk `
| Format-Table DeviceID,`
#{Name="Size(GB)";Expression={[decimal]("{0:N0}" -f($_.size/1gb))}}, `
#{Name="Free Space(GB)";Expression={[decimal]("{0:N0}" -f($_.freespace/1gb))}}, `
#{Name="Used Space(GB)";Expression={[decimal]("{0:N0}" -f(($_.size/1gb) - ($_.freespace/1gb)))}}, `
#{Name="Free (%)";Expression={"{0,6:P0}" -f(($_.freespace/1gb) / ($_.size/1gb))}} `
-AutoSize
}
catch
{
echo "Exception Occurred. Please try again on $servername"
}
echo "The total space on $servername are given below:"
echo $space
The desired result required was :
The total space on sw02014 are given below:
DeviceID Size(GB) Free Space(GB) Used Space(GB) Free (%)
-------- -------- -------------- -------------- --------
A: 0 0 0
C: 60 41 18 69 %
D: 100 78 22 78 %
E: 200 190 10 95 %
G: 0 0 0
Is there one line of code that will display free-size and disk-space of your logical disk in gb instead of mb? I tried doing some research but I couldn't find one liner, I did give this an attempt which was to divide it by 1GB, but that didn't work, how can I accomplish this?
gwmi win32_logicaldisk | Format-Table DeviceId, MediaType,Size,FreeSpace /1GB
Try calculated properties. I would also add [math]::Round() to shorten the values:
gwmi win32_logicaldisk | Format-Table DeviceId, MediaType, #{n="Size";e={[math]::Round($_.Size/1GB,2)}},#{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
n stands for name and e for expression. You could use the full names too, but it's a waste of space if you're writing multiple calculated Properties.
When performing any arithmetical calculation, it should be put in { }.
gwmi win32_logicaldisk | Format-Table DeviceId, MediaType,Size, {$_.FreeSpace /1GB}
You can read more on syntax from Microsoft Powershell Library
I'd like to provide an alternate/updated answer. (As of Powershell 5 at least, probably version 3.)
Just use Get-Volume
https://learn.microsoft.com/en-us/powershell/module/storage/get-volume
Example:
> get-volume
DriveLetter FriendlyName FileSystemType DriveType HealthStatus OperationalStatus SizeRemaining Size
----------- ------------ -------------- --------- ------------ ----------------- ------------- ----
FAT32 Fixed Healthy OK 451 MB 496 MB
C OSDISK NTFS Fixed Healthy OK 65.23 GB 474.3 GB
X Transfer_Shuttle NTFS Fixed Healthy OK 37.65 GB 48.68 GB
I have same issue and I want get KB,MB,GB,TB,PB on Server with Windows Server 2008 to 2016,
then I google many information to build a function:
function ConvertDoubleToBytes([double]$srcDouble){
if([Math]::Floor([Math]::Log($srcDouble,1024)) -eq 0){
$resn = ([Math]::Ceiling($srcDouble/([math]::pow(1024,([Math]::Floor([Math]::Log($srcDouble,1024)))))).ToString() + " Bytes")
} elseif ([Math]::Floor([Math]::Log($srcDouble,1024)) -eq 1){
$resn = ([Math]::Ceiling($srcDouble/([math]::pow(1024,([Math]::Floor([Math]::Log($srcDouble,1024)))))).ToString() + " KB")
} elseif ([Math]::Floor([Math]::Log($srcDouble,1024)) -eq 2){
$resn = ([Math]::Ceiling($srcDouble/([math]::pow(1024,([Math]::Floor([Math]::Log($srcDouble,1024)))))).ToString() + " MB")
} elseif ([Math]::Floor([Math]::Log($srcDouble,1024)) -eq 3){
$resn = ([Math]::Ceiling($srcDouble/([math]::pow(1024,([Math]::Floor([Math]::Log($srcDouble,1024)))))).ToString() + " GB")
} elseif ([Math]::Floor([Math]::Log($srcDouble,1024)) -eq 4){
$resn = ([Math]::Ceiling($srcDouble/([math]::pow(1024,([Math]::Floor([Math]::Log($srcDouble,1024)))))).ToString() + " TB")
} elseif ([Math]::Floor([Math]::Log($srcDouble,1024)) -eq 5){
$resn = ([Math]::Ceiling($srcDouble/([math]::pow(1024,([Math]::Floor([Math]::Log($srcDouble,1024)))))).ToString() + " PB")
}
return $resn
}
And I use it:
Get-WmiObject -query "Select * from win32_logicaldisk " | Select-Object DeviceID,#{n="FreeSpace";e={ConvertDoubleToBytes($([double]::Parse($_.FreeSpace)))}},#{n="Size";e={ConvertDoubleToBytes($([double]::Parse($_.Size)))}},#{n="Useage Percent";e={$([Math]::Ceiling((1-$($([double]::Parse($_.FreeSpace))/$([double]::Parse($_.Size))))*100)).ToString() + " %"}}
Will print like this:
DeviceID FreeSpace Size Useage Percent
-------- --------- ---- --------------
C: 36 GB 100 GB 65 %
D: 294 GB 600 GB 52 %
E:
F: 279 GB 600 GB 54 %
G:
H: 500 GB 500 GB 1 %
This works great.
$disks = get-wmiobject Win32_LogicalDisk -computername $computer -Filter "DriveType = 3"
foreach ($disk in $disks) {
$letter = $disk.deviceID
$volumename = $disk.volumename
$totalspace = [math]::round($disk.size / 1GB, 2)
$freespace = [math]::round($disk.freespace / 1GB, 2)
$usedspace = [math]::round(($disk.size - $disk.freespace) / 1GB, 2)
$disk | Select-Object #{n = "Computer Name"; e = { $computer } }, #{n = "Disk Letter"; e = { $letter } }, #{n = "Volume Name"; e = { $volumename } }, #{n = "Total Space"; e = { ($totalspace).tostring() + " GB" } }, #{n = "Free Space"; e = { ($freespace).tostring() + " GB" } }, #{n = "Used Space"; e = { ($usedspace).tostring() + " GB" } } | FT
}