Skip to content

Instantly share code, notes, and snippets.

@jimboy-701
Last active September 23, 2022 16:41
Show Gist options
  • Save jimboy-701/1300c0ab95cbe11b9ddfb650b3cabf29 to your computer and use it in GitHub Desktop.
Save jimboy-701/1300c0ab95cbe11b9ddfb650b3cabf29 to your computer and use it in GitHub Desktop.
Powershell scripting notes and snips

My PowerShell Scripting Notes

Running sequenced commands with && or || will usually result in the following error:

PS C:\Users\Example> echo Hello && echo World!

At line:1 char:12
+ echo Hello && echo World!
+            ~~
The token '&&' is not a valid statement separator in this version.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvalidEndOfLine

Do this instead; Command; if($?) {Command}

PS C:\Users\Example> echo "Hello"; if($?) {echo "World!"}

Hello
World!

Note: Since PowerShell Core v7, && and || can be used directly.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment