Freigeben über


The Joy of --%

I'm always learning with PowerShell. The other week I came across a little beauty!

 

--%

 

Stop Parsing

What's it do?

Well, anything after this symbol isn't parsed by PowerShell, i.e. it stops PowerShell from interpreting anything after as PowerShell commands or expressions.

 

Why use it?

Imagine you have to run a legacy executable from your PowerShell console. This executable has a particularly tricky syntax including some characters that PowerShell just loves to parse. How do you stop PowerShell interpreting these characters? Well, prior to v3 of PowerShell you'd have to escape the character with the, er, escape character, also know as 'grave' or 'back tick'.`

This would stop PowerShell parsing that particular character and made for some quite interesting syntax. With v3 and beyond you can use the stop parsing symbol. Anything after isn't parsed. Here's an example of how a dsacls command might look prior to v3:

dsacls "`"DC=contoso,DC=com`" /I:S /G `"NT AUTHORITY\SELF`":WP;msSVSAdmPwd;computer"

 

Nasty! Look at all of those escape characters...

And, here's the same command but with the benefit of --% :

dsacls --% DC=contoso,DC=com /I:S /G "NT AUTHORITY\SELF":WP;msSVSAdmPwd;computer

 

Still not the nicest of syntax - that's dsacls for you - but much, much less messing about to get it to work with PowerShell!

Comments

  • Anonymous
    July 03, 2015
    thanks