Display drive letter with disk space - powershell

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

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 do round memory size

I have this script line:
Get-WmiObject Win32_ComputerSystem | Select-Object TotalPhysicalMemory, #{Name="GB";Expression={$_.TotalPhysicalMemory/1GB}}
How to round this result 2 separate places after coma.
result at the moment is enter image description here
You can use the format string operator and specify the number of decimal places like this
Get-WmiObject Win32_ComputerSystem |
Select-Object TotalPhysicalMemory,
#{Name="GB";Expression={"{0:n2}" -f ($_.TotalPhysicalMemory/1GB)}}

Disk Space Check in 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)%)"
}

Powershell free disk space for drives without drive letter

I am working on a power shell script based on http://www.powershellneedfulthings.com/?p=36 to check the disk space for volumes that do not have a driver letter assigned.
The script works pretty well, but I'd like to filter that only drives are shown that have less than 10% free disk space. I'm running into troubles using the where-object filter with hash tables.
# calculations for displaying disk size information
$TotalGB = #{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1GB),2)}}
$FreeGB = #{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1GB),2)}}
$FreePerc = #{Name="Free(%)";expression={[math]::round(((($_.FreeSpace / 1GB)/($_.Capacity / 1073741824)) * 100),0)}}
# array declarations
$volumes = #()
# import server names to check
$servers = (Get-Content .\servers.txt)
# check disk space for volumes without drive letter
foreach ($server in $servers){
$volumes += Get-WmiObject -computer $server win32_volume | Where-Object {$_.DriveLetter -eq $null -and $_.Label -ne "System Reserved"}
}
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Format-Table -AutoSize
What I tried is:
Where-Object {$FreePerc -le 10}
The current output is:
SystemName Label Capacity(GB) FreeSpace(GB) Free(%)
---------- ----- ------------ ------------- ----
SERVER01 X:\data\ 9.97 0.89 9
SERVER01 X:\log\ 9.97 1.20 12
SERVER01 X:\info\ 9.97 3.49 35
I'd like to only show the volumes that have less than 10% free disk space. So in this case, only the first entry should be shown.
Thanks!
I think the where clause variable $FreePerc is the issue. Arco had the right idea.
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Where-Object {$_.'Free(%)' -le 10} | Format-Table -AutoSize
I put the property in single quotes because i think PowerShell would try to evaluate (%) otherwise. Also to make Arco's solution work it might just be easier to call the Name propery of $FreePerc. That way you only have to update one location
$volumes | Select SystemName, Label, $TotalGB, $FreeGB, $FreePerc | Where-Object {$_.($FreePerc.Name) -le 10} | Format-Table -AutoSize

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