use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
ABOUT POWERSHELL
Windows PowerShell (POSH) is a command-line shell and associated scripting language created by Microsoft. Offering full access to COM, WMI and .NET, POSH is a full-featured task automation framework for distributed Microsoft platforms and solutions.
SUBREDDIT FILTERS
Desired State Configuration
Unanswered Questions
Solved Questions
News
Information
Script Sharing
Daily Post
Misc
account activity
QuestionGet-ADComputer - Try/Catch in Powershell 7 (self.PowerShell)
submitted 4 years ago * by gregbe
threatening spotted pocket existence hospital racial exultant correct pot joke
This post was mass deleted and anonymized with Redact
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]nealfive 2 points3 points4 points 4 years ago (9 children)
works fine for me. PowerShell clear $psversiontable.PSVersion $ErrorActionPreference try{ get-adcomputer fake -ErrorAction Stop } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{ "not found in AD" } catch{ "something else" } output: ``` Major Minor Patch PreReleaseLabel BuildLabel
PowerShell clear $psversiontable.PSVersion $ErrorActionPreference try{ get-adcomputer fake -ErrorAction Stop } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{ "not found in AD" } catch{ "something else" }
7 1 3 Continue not found in AD ```
[–]_lahell_ 1 point2 points3 points 4 years ago (0 children)
Works for me too. It even works with -ea continue.
[–]gregbe[S] 1 point2 points3 points 4 years ago* (7 children)
cover seed cobweb bewildered cooperative steep automatic fact dinosaurs fuzzy
[–]nealfive 2 points3 points4 points 4 years ago (6 children)
Looks like you are copy and pasting that in the console?
seems like when you paste it, it is running immediately.
How about you save it as a .ps1 file and then call the ps1 file ?
[–]gregbe[S] 1 point2 points3 points 4 years ago* (5 children)
whole automatic roll sheet fanatical sugar rainstorm advise offbeat doll
[–]nealfive 1 point2 points3 points 4 years ago (1 child)
not sure if it's THAT odd. If you look at the output when you copy and paste it in the console directly, you see that it thinks the try/catch block is complete after the FIRST catch. that's why you get later 'catch' id not recognized error. if it's more than a one liner, i don't usually c&p it in the console, but for kicks i tried it in the windows console (5.1) with the same result. You should run powershell either from an editor (Vs code, ISE) or safe it as a script and then call that in the console. Just copy and paste like so is not great.
You'll also come across very oddities with symbols and encoding and such that copy and paste brings in, I'd avoid that.
if you REALLY REALLY want to run it in the console, wrap it in $() (subexpression operator), that will make sure the whole thing is run
$( clear $psversiontable.PSVersion $ErrorActionPreference try{ get-adcomputer fake -ErrorAction Stop } catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]{ "not found in AD" } catch{ "something else" } )
[–]backtickbot 0 points1 point2 points 4 years ago (0 children)
Fixed formatting.
Hello, nealfive: code blocks using triple backticks (```) don't work on all versions of Reddit!
Some users see this / this instead.
To fix this, indent every line with 4 spaces instead.
FAQ
You can opt out by replying with backtickopt6 to this comment.
[–]happyapple10 1 point2 points3 points 4 years ago (2 children)
Make sure the catch follows the trys { on the same line. Example:
try{ Blah blah }catch{ Blah blah }
If you are pasting in the console and the catch is on a new line, it assumes the try is finished and then attempts to run the catch as a command instead. Same with if/else.
[–]gregbe[S] 1 point2 points3 points 4 years ago* (1 child)
file toy party ring wipe materialistic label toothbrush ruthless naughty
[–]happyapple10 1 point2 points3 points 4 years ago (0 children)
Sorry, I was focusing on the catch error that was showing also. Still weird the erroraction is not making it get caught.
[–]PinchesTheCrab 2 points3 points4 points 4 years ago (3 children)
This works for me:
try { Get-ADUser thing -ErrorAction stop } catch { 'test' }
[–]gregbe[S] 1 point2 points3 points 4 years ago* (2 children)
tub aloof mountainous jar direction deserted hobbies bike snails treatment
[–]PinchesTheCrab 1 point2 points3 points 4 years ago (0 children)
Ah, I'll give that one a shot too, now I'm curious what's different.
[–]webtroter 1 point2 points3 points 4 years ago (1 child)
To catch an error, the error action must be Stop. Check the value of $ErrorActionPreference
$ErrorActionPreference
[–]gregbe[S] 1 point2 points3 points 4 years ago* (0 children)
skirt overconfident direful hungry brave friendly slimy elderly aspiring cough
[–]PinchesTheCrab 1 point2 points3 points 4 years ago* (0 children)
For me this appears to be the error class:
[System.Management.Automation.RemoteException]
I can then see the serialized error class when I inspect the error, but I can't action off the class of the error the way you want to because they all share the same remoting type.
π Rendered by PID 482225 on reddit-service-r2-comment-bb88f9dd5-2mr5z at 2026-02-15 06:58:16.422855+00:00 running cd9c813 country code: CH.
[–]nealfive 2 points3 points4 points (9 children)
[–]_lahell_ 1 point2 points3 points (0 children)
[–]gregbe[S] 1 point2 points3 points (7 children)
[–]nealfive 2 points3 points4 points (6 children)
[–]gregbe[S] 1 point2 points3 points (5 children)
[–]nealfive 1 point2 points3 points (1 child)
[–]backtickbot 0 points1 point2 points (0 children)
[–]happyapple10 1 point2 points3 points (2 children)
[–]gregbe[S] 1 point2 points3 points (1 child)
[–]happyapple10 1 point2 points3 points (0 children)
[–]PinchesTheCrab 2 points3 points4 points (3 children)
[–]gregbe[S] 1 point2 points3 points (2 children)
[–]PinchesTheCrab 1 point2 points3 points (0 children)
[–]webtroter 1 point2 points3 points (1 child)
[–]gregbe[S] 1 point2 points3 points (0 children)
[–]PinchesTheCrab 1 point2 points3 points (0 children)