How can I catch errors in my PowerShell script for querying disk space?

August 18, 2025 5 answers
Asked by filament.1p75
I'm working on a PowerShell script that checks the free space on a specific drive across all domain controllers in our setup. I've encountered a problem recently because our site-to-site link is down, which means I can't reach one of the DCs. I'm trying to handle the error that gets thrown when the script fails to connect to that particular DC, but I can't seem to get the error handling to work correctly. I'm using a `try/catch` block, but the error still interrupts the script. Here's the line that generates the error: `Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID='D:'" -ComputerName $allDCs`. Can anyone guide me on how to properly catch this error and provide a message indicating the connection issue?

Answers

~windGust31
You can modify your command by adding `-ErrorAction Stop` directly in your `Get-CimInstance` line. This will force it to throw a terminating error for failures, making it compatible with `try/catch`. Try this: `$allDisks = @(Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID='D:'" -ComputerName $allDCs -ErrorAction Stop)`
Reply to this answer
tournament seeding
To handle this, you should set the error action for the `Get-CimInstance` command. Using `-ErrorAction Stop` will make the errors terminating, allowing them to be caught in your `try/catch` block. Check out this link for more details on error handling in PowerShell! [Microsoft Blog on ErrorAction](https://devblogs.microsoft.com/powershell/erroraction-and-errorvariable/)

tilt.shift: Thatโ€™s right! PowerShell has different levels of errors. Using `-ErrorAction Stop` is the best approach to ensure that your `try/catch` handles it properly. Just be aware that some errors can slip through the cracks due to the layers of code involved.

cider press: Thanks for sharing the link! I'm definitely going to read up on it after lunch.

Reply to this answer
dinner is ready~
While `-ErrorAction` is useful, remember that it stops at the first error. You might want to capture all errors instead of stopping the script. Use `-ErrorVariable` to collect errors like this: `$allDisks = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID='D:'" -ComputerName $allDCs -ErrorVariable DiskErrors`. Then you can work with the collected errors later.

wrong platform: This method looks promising! Iโ€™ll give it a try. Thanks for the suggestion!

syntax.tree.leaf: Good find! Also, keep in mind that if errors are reported and you don't want them to show on the host, you can use `-ErrorAction SilentlyContinue`.

Reply to this answer
glacier__survey
That's a smart approach! By testing reachability first, you'll keep your output clean and won't get bogged down with too many error messages.
Reply to this answer
detour~4km
Another option is to check for connectivity issues before querying disk space. You can use `Test-Connection` or `Test-NetConnection` to make sure you can reach the DC before running `Get-CimInstance`. This way, you'll avoid handling multiple errors later on.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions