Intune Drift Control with PowerShell, Pipeline Error Handling Tips
Hello, fellow PowerShell Engineers! I hope you all are having a great summer. Here is this week’s roundup of PowerShell content.
PowerShell Tip: PowerShell Pipeline Error Handling
Pipelines can fail at any stage, but with these three error-handling techniques, you can build resilient PowerShell workflows that gracefully handle problems:
1. Try-Catch with Pipeline Continuation
Get-ChildItem C:\Data -Recurse | ForEach-Object {
try { Get-Content $_.FullName -ErrorAction Stop }
catch { Write-Warning "Failed to read: $($_.Name)" }
} | Where-Object { $_ -ne $null }
This pattern wraps risky operations in try-catch blocks within the pipeline, allowing processing to continue even when individual items fail. Failed reads are logged as warnings, while successful content flows through.
2. Error Variable Collection Pipeline
Get-Service | Stop-Service -ErrorAction SilentlyContinue -ErrorVariable StopErrors |
Start-Service -ErrorAction SilentlyContinue -ErrorVariable StartErrors |
ForEach-Object { if ($StopErrors -or $StartErrors) { Write-Host "Service restart issues detected" } }
Collect errors from multiple pipeline stages using -ErrorVariable
. This lets you handle all accumulated errors at the end while keeping the pipeline flowing smoothly.
3. Conditional Pipeline Branching
Get-WmiObject Win32_LogicalDisk | Where-Object DriveType -eq 3 |
ForEach-Object { if ($_.FreeSpace -lt 1GB) { $_ | Select-Object DeviceID, @{N='Status';E={'Critical'}} }
else { $_ | Select-Object DeviceID, @{N='Status';E={'Healthy'}} } } |
Group-Object Status
Use conditional logic within pipeline stages to branch processing based on data conditions. This example categorizes disk drives by free space and groups the results by health status.
Always consider what happens when your pipeline encounters unexpected data or system states. These patterns help you build PowerShell scripts that fail gracefully and provide useful feedback when things go wrong.
PowerShell Videos
Andrew sits down with Anthony Powell, the Posh Wolf, to discuss his PowerShell journey from his early days in a two-person IT shop, to presenting at the PowerShell Summit.
YouTuber CryptoW@re shares a real world example of debugging a malicious PowerShell Script.
Frank Lesniak’s presentation on Unleashing the Power of Cross-Platform PowerShell is now on YouTube!
PowerShell Community News and Projects
How to Block Ad-Hoc Email-Based Subscriptions
In this article, author Tony Redmond, talks about how the deprecation of the MSOL and AzureAD PowerShell modules impacts blocking ad-hoc email-based subscriptions for services like Copilot Studio, and explains that the Entra ID authorization policy is the current method to achieve this. He demonstrates how to use the Update-MgPolicyAuthorizationPolicy
cmdlet to block these subscriptions, noting that there isn't a direct one-to-one replacement for the old Set-MsolCompanySettings
cmdlet.
Read More: How to Block Ad-Hoc Email-Based Subscriptions
Microsoft shares script to restore inetpub folder you shouldn’t delete
Sergiu Gatlan has an important article on Microsoft's release of a PowerShell script to restore the 'inetpub' folder, which was unexpectedly created by April 2025 Windows security updates and should not be deleted as it mitigates a high-severity privilege escalation vulnerability. The article explains that deleting this folder makes systems vulnerable again, and the script helps recreate it with correct permissions.
Read More: Microsoft shares script to restore inetpub folder you shouldn’t delete
Intune Drift Control PowerShell
Niels Kok has a great article on building a PowerShell-based configuration drift control solution for Microsoft Intune using the Pester module, allowing users to compare client configurations against a defined baseline for DeviceConfiguration, DeviceCompliance, and SettingsCatalog policies. This solution offers an alternative to hosted tools and can be integrated into configuration as code pipelines.
Read More: Intune Drift Control PowerShell
Download Windows Themes using PowerShell
Harm Veenstra has an interesting article on using PowerShell to download Windows themes from the Microsoft website, allowing users to save them locally for easy switching. While Microsoft's theme download page is becoming obsolete, this script offers a way to retrieve and manage these .Themepack
files before they're gone.
Read More: Download Windows Themes using PowerShell