Disk Space Check in Powershell - powershell

I am currently checking disk space size using power shell but was wondering how I can amend the below to add a percentage of space left?
Format-Table DeviceId, MediaType, #{n="Size";e={[math]::Round($_.Size/1MB,2)}},#{n="FreeSpace";e={[math]::Round($_.FreeSpace/1MB,2)}}
I have tried to do it by changing the math but no luck.
gwmi win32_logicaldisk -Computername PCNAME| Format-Table DeviceId, MediaType, #{n="Size";e={[math]::Round($_.Size/1MB,2)}},#{n="FreeSpace" (%);e={[math]::Round($_.FreeSpace/$_.Size*100,2)}}|Out-File c:\PMC\Disk\OutPut\Newcastle.txt
This just returns an error.

Here is an example how to do it:
gwmi Win32_LogicalDisk -Filter "DeviceID='C:'" | 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}} -AutoSize
output:
Name FS Free, Gb Free,%
---- -- -------- ------
C: NTFS 593.59 31.88

Here's part of a script I have written that may help you:
If (Test-Path 'C:')
{
$CDisk = GWMI Win32_LogicalDisk -Filter "DeviceID='C:'"
$CDisk = #{'Size' = [Math]::Round($CDisk.Size / 1GB);
'FreeSpace' = [Math]::Round($CDisk.FreeSpace / 1GB)}
$CDisk.Add('Usage', ($CDisk.Size - $CDisk.FreeSpace))
$CDisk.Add('PercentUsage', [Math]::Round(($CDisk.Usage / $CDisk.Size) * 100))
"C: drive free space: $($CDisk.FreeSpace)GB"
"C: drive capacity: $($CDisk.Size)GB"
'--------------------------------'
"Disk usage: $($CDisk.Usage)GB ($($CDisk.PercentUsage)%)"
}

Related

Stop Powershell Script if disk size is less than X

I wonder if you could help me out guys.
I need to stop a powershell script if the disk size of partition E is less than 10GB, and to continue if it´s more than 10GB.
So far i managed to get my disk size listed with this.
Get-WmiObject -Class win32_logicaldisk | Format-Table DeviceId,#{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
And i get this result:
DeviceId
Freespace
A
0
C
77.9
D
0
E
34.05
So, i want to stop the powershell script if E unit has less than 10GB.
How can i do it?
Thanks in advance
If you want to put the Freespace of E in a variable you can do this :
$VarSpace = $(Get-WmiObject -Class win32_logicaldisk | Where-Object -Property Name -eq C:).FreeSpace/1GB
then you can do a simple if for check :
if ($VarSpace -le 10){ <Something for stopping you script like exit> }
You can use the Get-Volume cmdlet for that or Get-CimInstancerather than the old Get-WmiObject:
$freeOnE = (Get-CimInstance -ClassName win32_logicaldisk | Where-Object {$_.DeviceID -eq 'E:'}).FreeSpace / 1GB
or
$freeOnE = (Get-Volume -DriveLetter E).SizeRemaining / 1GB
Then exit your PowerShell session if this value is below 10Gb
if ($freeOnE -lt 10) { exit }

How to exclude part of an output in Powershell?

i am writing a script that accepts the device ID as an argument to check the used percentage of a disk. Here is my code.
$device_id = $args[0]
Get-WmiObject -Class Win32_LogicalDisk |
Select-Object -Property DeviceID,
#{label='UsedPercentage'; expression={[Math]::Round((($_.Size - $_.FreeSpace)/$_.Size) * 100, 2)}} |
findstr $device_id
Here is my output. i am passing an argument to see usage of the device by device ID.
PS D:\Development\Powershell> .\disk-usage.ps1 D:
D: 57.69
What i want to do is to just output that number. How do i do this?
There's no need to use findstr to filter the output. Instead, use the parameter argument to filter your WMI query:
$device_id = $args[0]
# use argument to filter WMI query
Get-WmiObject -Class Win32_LogicalDisk -Filter "DeviceID = '$device_id'" |ForEach-Object {
# output the free space calculation, nothing else
[Math]::Round((($_.Size - $_.FreeSpace)/$_.Size) * 100, 2)
}
You can add the 'used percentage' as a property to the WMI object you get back from your query:
$deviceID = args[0]
$diskUsage = Get-WmiObject -Query "SELECT FreeSpace, Size FROM Win32_LogicalDisk WHERE DeviceID = '$deviceID'" |
Add-Member -MemberType ScriptProperty -Name 'UsedPercentage' -Value {[Math]::Round((($this.Size - $this.FreeSpace)/$this.Size) * 100, 2)} -PassThru
Now, $diskUsage is a WMI object with Size, FreeSpace and UsedPercentage properties (as well as some WMI metadata properties you can ignore). You can output the value of any of them by refering to the one you want:
$diskUsage.UsedPercentage
15.3
Or show them in a neat table:
$diskUsage | Format-Table Size, FreeSpace, UsedPercentage -AutoSize
Size FreeSpace UsedPercentage
---- --------- --------------
1013310287872 858247196672 15.3

How to get get-volume output in % and how to check used RAM in % in Powershell

I have tried using Get-volume to output to a file and it works great.
Is there any way to get result in % and is there a way to show used ram?
You have to calculate the free disk space by yourself like:
Get-Volume | Select-Object -Property *, #{name="FreeSpace"; expression={ "$(100 / $_.Size * $_.SizeRemaining)%" }}
Use Get-Ciminstance Win32_OperatingSystem to get information about the Operating System like RAM usage:
$osData = Get-Ciminstance Win32_OperatingSystem
$osData.FreePhysicalMemory
$osData.TotalVisibleMemorySize

Display drive letter with disk space

I'm just trying to display the drive letter with the freespace from this. I'm pretty sure I need to include it in the ForEach, but not sure how. Right now all I get is the free disk space.
Get-WMIObject Win32_LogicalDisk -Filter "DeviceID='C:' or DeviceID='D:' or DeviceID='L:'" | ForEach-Object {[math]::truncate($_.freespace / 1GB)}
33
33
33
Try this:
Get-WMIObject Win32_LogicalDisk -Filter "DeviceID='C:' or DeviceID='D:' or DeviceID='L:'" | Select DeviceID,#{N='FreeSpace';E={[math]::truncate($_.freespace / 1GB)}}
DeviceID FreeSpace
-------- ---------
C: 75
D: 0
Instead of ForEach-Object, use Select-Object and place your free space calculation inside a calculated property expression instead:
Get-WmiObject ...| Select-Object DeviceID,#{Name='FreeSpace';Expression={[math]::Truncate($_.FreeSpace / 1GB)}

Unable to retrieve physical size of available storage for cluster

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)}}