If you’re searching for a way to visualize folder structures similar to WinDirStat, but using PowerShell, you’re in the right place. In many production environments, installing third-party tools may not be allowed due to security or compliance policies. However, you can still gather detailed disk usage information using PowerShell. This method provides an efficient way to analyze folder sizes without installing any additional software.
Why Use PowerShell?
PowerShell is a built-in tool on Windows systems and offers powerful commands to automate and analyze tasks directly. By leveraging PowerShell, you can easily gather disk usage information and visualize folder sizes in a tabular format. This approach is not only lightweight but also perfect for production environments where the use of third-party tools is restricted.
Step-by-Step Guide: Visualising Folder Sizes with PowerShell
Here’s how you can create a script that lists folder sizes, sorted from largest to smallest, giving you a view similar to what tools like WinDirStat provide:
# Define the path to analyze
$path = "C:\"
# Get all directories and calculate their sizes
$folderSizes = Get-ChildItem -Path $path -Directory -Recurse |
ForEach-Object {
try {
$size = (Get-ChildItem -Path $_.FullName -Recurse -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
[PSCustomObject]@{
FolderPath = $_.FullName
SizeMB = [math]::Round($size / 1MB, 2)
SizeGB = [math]::Round($size / 1GB, 2)
}
} catch {
# Handle errors, if any
}
} | Sort-Object -Property SizeGB -Descending
# Display the output in a table format
$folderSizes | Format-Table -Property FolderPath, SizeMB, SizeGB -AutoSize
# Optional: Export to CSV
# $folderSizes | Export-Csv -Path "C:\folder_sizes.csv" -NoTypeInformation
Script Explanation
- $path: Set this variable to the drive or folder you want to analyze. For example, "C:\" scans the entire C drive.
- Get-ChildItem: This command retrieves all directories recursively under the specified path.
- ForEach-Object: Iterates through each folder, calculating its total size.
- [PSCustomObject]: Creates a structured object for each folder, including its path and size in MB and GB.
- Sort-Object: Sorts folders by size, displaying the largest first.
- Format-Table: Displays the output as a table directly in the PowerShell console.
Why This Script is Useful
• No Third-Party Tools Required: This method is perfect for environments where you can’t install external software.
• Efficient and Automated: PowerShell provides a flexible and powerful way to gather information and automate tasks.
• Customizable Output: You can modify the script to suit specific needs, such as exporting data to CSV for further analysis.
Tips for Running the Script
- Run PowerShell as Administrator: This script may require administrative permissions to access protected folders.
- Be Patient: Scanning large drives can take some time. The script is designed to skip over restricted folders and continue processing.
- Exporting the Results: You can uncomment the Export-Csv line to save the results to a CSV file, making it easier to share or analyze the data further.