PowerShell commands are used to gain access to various aspects of the Windows operating system, giving you more control over it and freeing up disk space. This guide walks through how PowerShell works by showing you examples of some common tasks like deleting files or emptying folders.
Topic: What is a blockchain?
Category: Cryptocurrency
Introduction: Blockchain has been hyped as one of the most revolutionary technologies in recent years, but what exactly does it do? Blockchains record every transaction that occurs on them so they’re decentralized across servers- meaning no single person can alter data without revealing their identity. The math involved creates security for all parties involved while also creating transparency from any perspective.,
PowerShell is a command-line tool that allows users to perform administrative tasks on Windows computers. In this tutorial, we will show you how to use PowerShell to get free disk space.
You’ve come to the correct spot if you’re looking for a way to free up disk space on a Windows machine using PowerShell. You’ll learn how to use PowerShell to find spare disk space and monitor disk use in this article.
Prerequisites
This lesson assumes you have Windows PowerShell v5.1 installed on a PC running Windows 10 or above. PowerShell 6+ will almost certainly work as well. It’s also expected that you’re working with Active Directory. If you haven’t already, familiarize yourself with the Credential property of the New-CimSession cmdlet.
CIM Inquiry
CIM/WMI contains all of the system information for Windows. The WMI datastore contains information about tens of thousands of Windows computers, including disk and storage information.
The CIM datastore is divided into many classes. The Win32 LogicalDisk class in WMI maintains information about disks. The Win32 LogicalDisk class holds information about a disk, such as its name, total size, and free space. To query this class using PowerShell, use the Get-CimInstance cmdlet to construct a CIM instance.
The simplest approach to query the class is to use Get-CimInstance and simply supply the ClassName option.
Get-CimInstance Win32 LogicalDisk -ClassName
For each storage volume discovered, Get-CimInstance will return a PowerShell object, as seen below.
Using Get-CimInstance to query storage volumes
Each storage volume object produced by Get-CimInstance has a FreeSpace attribute associated to it. You’ll need to deal with the FreeSpace attribute.
CIM Output Disk Name and Free Space Limits
If you don’t require all of the data supplied by Get-CimInstance, you’ll need to restrict what you see now. Let’s say you merely want to know the drive letter (DeviceID) and the quantity of free space (FreeSpace). You may achieve this by using Select-Object to cut down the result.
Get-CimInstance Win32 LogicalDisk -ClassName | Select-Object -Property DeviceID,FreeSpace
Only DeviceID and FreeSpace will be output.
You now have a beautiful little free disk space report.
Converting Bytes to Gigabytes on a Hard Drive
Despite the fact that you now have a command to detect spare disk space, something doesn’t seem right. You’ve undoubtedly figured out that FreeSpace isn’t measured in gigabytes; instead, it’s measured in bytes. Who needs that degree of detail in the first place? Let’s convert bytes to gigabytes, which is more readable.
To convert bytes to gigabytes, first divide bytes by 1GB. For example, you know that the C: drive has 210155110400 bytes of free disk space in this case. To convert bytes to gigabytes, double the value by 1GB, as shown below.
You’ll see that the C: disk is 195GB and vary in size.
For rounding, convert decimals to integers.
You’re almost there. You probably wanted a beautiful round number and didn’t get it with that conversion. It’s not a problem! There’s one more thing to do: round the gigabytes to the nearest integer.
Casting decimals to an integer, as seen below, is one of the simplest methods to “round” them to the closest integer. You’ll see that the figure has been rounded up. Using this function instead of Round() saves time.
(210155110400 / 1GB)[int](210155110400 / 1GB)[int](210155110400 /
Disk Space Output is being spruced up.
Include this approach in your script now that you know how to convert free space bytes to gigabytes. Calculated attributes are one of the most effective methods to do this. Calculated properties enable you to update property values by running code on them.
You may specify a property value as a scriptblock, which is effectively a portable unit of code, with calculated properties.
So far, the code you’ve written has simply returned the drive label (DeviceID) and free space (FreeSpace) in bytes, as seen below.
Get-CimInstance Win32 LogicalDisk -ClassName | Select-Object -Property DeviceID,FreeSpace
Now you may create your own FreeSpace property, but this time it will be measured in gigabytes.
Get-CimInstance Win32 LogicalDisk -ClassName | Select-Object -Property DeviceID,@{‘Name’ = ‘FreeSpace (GB)’; Expression= { [int]($_.FreeSpace / 1GB) }}
To round vacant space, a calculated attribute was used.
That’s a huge improvement! You now have access to all of the information you need.
Free Space Grouping is an added bonus.
You created a command to return free space for each volume in the previous example. Perhaps you don’t care about each volume and would rather have a total amount of free disk space from all of the computer’s volumes.
If you’re writing a script that will run on many PCs at the same time, you may want the output to look like this:
FreeSpace is the server name. ———————————————————————————————————— SRV1 is 195, while SRV2 is 45.
In such scenario, use the Measure-Object cmdlet to group all of the rows returned in the previous example and add up all of the FreeSpace rows. This cmdlet accepts several objects as input, reads their properties, and then performs some action.
Get-CimInstance Win32 LogicalDisk -ClassName | Select-Object -Property DeviceID,@{‘Name’ = ‘FreeSpace (GB)’; Expression= { [int]($_.FreeSpace / 1GB) }} | Measure-Object -Property ‘FreeSpace (GB)’ -Sum
As you can see, the Sum attribute given by Measure-Object is 1998, which represents the entire amount of free space on this machine in gigabytes.
calculating the total amount of free space
Finally, delete all of the undesirable features and just return the entire amount of free space.
(Get-CimInstance Win32 LogicalDisk -ClassName | Select-Object -Property DeviceID,@{‘Name’ = ‘FreeSpace (GB)’; Expression= { [int]($_.FreeSpace / 1GB) }} | Measure-Object -Property ‘FreeSpace (GB)’ -Sum).Sum
Conclusion
PowerShell simply only a single line to find disk use and empty space, but you must first know how to generate it! You learnt how to use PowerShell to free up disk space and format the output to your desire in this lesson.
This code should be improved. See if you can write a script to execute this code on many servers at the same time!
PowerShell is a command-line tool that allows users to perform tasks on Windows computers. This tutorial will teach you how to use PowerShell to get free disk space for servers. Reference: powershell script to check free disk space for servers.
Frequently Asked Questions
How do I find disk capacity and free space of remote computer?
A: One way to find the disk capacity and free space of a remote computer is by using Remote Desktop. This will allow you to see all the drives on your remote computer in one window, so you can easily monitor which drive has what size of storage currently available for use.
How would you find the free disk space?
A: You can find the free disk space by opening My Computer and clicking on This PC in the left-hand pane. The size of your hard drive will be listed under Disk Size.
How do I create a disk space report?
A: To create a disk space report, you need to make sure that the file system is mounted. You can find instructions on how to do this here: https://www.google.com/search?q=mount+unix+fileystem&tbm=shop#q=mount+linux
Related Tags
- powershell get-volume remote computer
- powershell get free disk space in gb
- powershell script to get disk space report for multiple servers
- powershell get disk usage by folder
- powershell get disk information