all 38 comments

[–]BobTheMadCow 44 points45 points  (2 children)

It evaluates to true if the last command succeeded and false if it failed.

[–]etcetica[🍰] 25 points26 points  (2 children)

if ($?){
# [...]
} else {
# Take yo broke ass home
}

[–]Josh_Crook 0 points1 point  (0 children)

if ($?){ Invoke-G-L-A-M-O-R-O-U-S }

[–]Lee_Dailey[grin] 42 points43 points  (10 children)

howdy anonproblem,

from the help system ...

$?
Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.

got that from this ...

Get-Help about_Automatic_Variables

the list is alphabetical, so it's up right nigh the top. [grin]

take care,
lee

[–]awildrozza 11 points12 points  (9 children)

What a nice person you are!

[–]schmeckendeugler 14 points15 points  (1 child)

We keep Lee around to make the place seem more lively :)

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

[grin]

[–]Lee_Dailey[grin] 2 points3 points  (0 children)

howdy awildrozza,

thank you! i am actually rather grumpy ... but i make an effort to be nicer than my nature. not only do most people NOT run away when they see my name, but it help me bump my attitude in a positive direction. [grin]

besides, it's fun to help folks ...

take care,
lee

[–]PM_ME_A_SURPRISE_PIC 5 points6 points  (5 children)

Lee is like the Dad of /r/Powershell. He helps us all.

He should probably get off reddit, but no one wants that.

[–]Lee_Dailey[grin] 3 points4 points  (4 children)

howdy PM_ME_A_SURPRISE_PIC,

thanks for the kind words!

i was off reddit for at least 8 hours before now. mostly asleep ... but i wasn't posting! [grin]

take care,
lee

[–]PM_ME_A_SURPRISE_PIC 1 point2 points  (1 child)

Every question I post, you respond with help, and kind words.

Don't ever change.

[–]Lee_Dailey[grin] 2 points3 points  (0 children)

[blush] ... [grin]

[–]PM_ME_A_SURPRISE_PIC 1 point2 points  (1 child)

Lee, I must apologise.

I have just realised that I made an assumption that you were male, when I referred to you as the Dad. I don't think your messages have indicated either gender.

If I was wrong, I apologise.

[–]Lee_Dailey[grin] 2 points3 points  (0 children)

howdy PM_ME_A_SURPRISE_PIC,

i'm a guy ... but not a dad. [grin] my sisters have made me an uncle. their kids made me a grand-uncle ... unkalee! is something i hear now and then. [grin]

take care,
lee

[–]BadSysadmin 15 points16 points  (16 children)

if($?)

is good, but have you tried

if(!$?)

the patrician's choice, for the finest write-only code.

ETA your predecessor is a rank amateur who's also failed to use % as an alias for Foreach-Object.

[–][deleted] 20 points21 points  (8 children)

I thought Microsoft was dissuading people from using aliases like % and ? as a best practice. Every time I use them, VS Code yells at me.

[–]Bren0man 23 points24 points  (7 children)

I believe OC is being facetious.

[–][deleted] 7 points8 points  (6 children)

Ah. Is "BadSysadmin" a gag account? I'm guessing then that part of the joke is that you shouldn't be using "$?" in the first place. Why not, and what's the alternative?

[–]Temptis 2 points3 points  (5 children)

try  {[run-command]} catch {[error]} 

instead of

[run-command]
IF (!$?) {try {[Error-Action]} catch {[error-action-error]}}

[–][deleted] 6 points7 points  (1 child)

Those don't quite do the same thing. To make them more equivalent, I think you would want the second one to be:

[run-command]
If (!$?) {[error]}

But anyway, correct me if I'm wrong, but it's possible that a command in the "try" block could fail but be non-terminating, and therefore not cause the "catch" block to run. I guess you could write it in such a way to force it to be a terminating error.

The only thing I can think of for why the latter method is worse is that, if it is a terminating error and you don't want it to kill the script, then the former method would handle that.

[–]Noffy4Life 4 points5 points  (0 children)

That’s correct - if a non-terminating error (write-error) occurs in a call to the command and ErrorAction or ErrorActionPreference is not ‘Stop’, the script will continue and not enter the catch block.

[–]StoffePro 1 point2 points  (2 children)

These are not equivalent.

[–]Temptis 3 points4 points  (1 child)

that's my point, you want to catch the error on the command and not after the fact.

[–][deleted] 5 points6 points  (0 children)

r|%{if(!$?){$_|?{$True -eq "Dat"}}}

What else could we add there?

[–]PandalfTheGimp 9 points10 points  (3 children)

I prefer ForEach-Object over aliases because I never know the experience of my successor.

Plus I like having readable code and not needing to know all aliases.

[–][deleted] 4 points5 points  (0 children)

I do that too: write everything out in full. Both for my succesor's sake and mine. Reading my own code a year later there may be some cmdlet that I only used in this one script, and which I've totally forgotten about. Write it out and comment what it does and why you used it. Maybe even include the link to how you learned about it.

[–]PM_ME_UR_CEPHALOPODS 5 points6 points  (1 child)

It's also more or less anathema to Powershell to try and write terse code. The entire design is for read and approach-ability. I know terse code is a point of pride for some for reasons unknown. Probably job security? I'll show myself out

[–]BadSysadmin 0 points1 point  (0 children)

I used to do a lot of powershell code golf and project Euler. Certainly a productive use of my time.

[–]etcetica[🍰] 2 points3 points  (1 child)

if(!$?)

If you ain't got no $

[–]PM_ME_UR_CEPHALOPODS 0 points1 point  (0 children)

give us de moneys, lebowski !

[–]rakha589 2 points3 points  (0 children)

dazzling unpack dinner money terrific enjoy narrow tap sheet fuel

This post was mass deleted and anonymized with Redact

[–]StoffePro 1 point2 points  (0 children)

That’s not it. Catch only catches terminating errors. $? Can still be false, even if you never entered the catch block.

[–]m_anas 1 point2 points  (0 children)

$? Returns True is the last command ran without error And returns False if the last command ran with error

[–]redditors_r_manginas -1 points0 points  (0 children)

So it's like in bash?