We have a powershell script that requires escaping the * character.
PS C:\test> .\jre\bin\java.exe -cp .\test.jar com.test.a123 h256 *
I want to escape above star....
Unable to get any clue in google, any idea on this please?
The generic PowerShell escape character is a grave (`), although I must admit, I can't think of a use case where you would need to escape an asterisk. Remember you can always use string literals too:
#'
String literal here
'#
Related
I am working with Powershell. My issue is that my file path (which does not exist on a local computer) has an apostrophe in it. Powershell is seeing this as a single quote, so it is giving me the following error: The string is missing the terminator: '. I thought that I could escape the single quote using a backtick, but that gave me the same error.
The error does not occur when I am doing the first line of code, and I don't even need the backtick for that part. I can even see that the contents of the variable matches up with the file path that I am using. It is only when I am doing the invoke-expression part that it is giving me the error.
I am using https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-expression?view=powershell-7, so I don't think the second line of the code is the problem.
My code is listed below:
$code = "\\example\example\John_Doe`'s_Folder\example.ps1"
invoke-expression -command $code
I have also tried wrapping the entire file path in double-quotes and single-quotes, but my program did not like that either. I can't remove the apostrophe as we have over a hundred of systems that are directing to John_Doe's_Folder.
Invoke-Expression should generally be avoided; definitely don't use it to invoke a script or external program.
In your case, simply use &, the call operator to invoke your script via the path stored in variable $code (see this answer for background information), in which case the embedded ' needs no escaping at all:
$code = "\\example\example\John_Doe's_Folder\example.ps1"
& $code
As for what you tried:
"\\example\example\John_Doe`'s_Folder\example.ps1" turns into the following verbatim string content:
\\example\example\John_Doe's_Folder\example.ps1
That is, the ` was removed by PowerShell's parsing of the "..." string literal itself, inside of which ` acts as the escape character; since escape sequence `' has no special meaning, the ` is simply removed.
For the ` to "survive", you need to escape the ` char. itself, which you can do with ``:
"\\example\example\John_Doe``'s_Folder\example.ps1"
I have a DOS batch file that has a line that executes a powershell script. First I tried a very simple script with this line in the batch file:
powershell -command "get-date" < nul
That worked great. But the script has nested double-quote characters, which can sometimes be escaped with a backtick (`) character. So then I tried this:
powershell -command "Write-Host `"hello world`"" < nul
That also worked great. However, the script I need to run is pretty complicated and has more than one level of nested double-quote characters. I have taken the complicated script and simplified it to an example that has the same principles here:
[string]$Source = " `"hello world`" ";
Write-Host $Source;
If I save this script inside a PS script file and run it, it works fine, printing out “hello world” including the double quotes, but I need to embed it in the line in the batch file. So I take the script and put it all on one line, and try to insert it into the batch file line, but it doesn’t work. I try to escape the double-quotes, but it still doesn’t work, like this:
powershell -command "[string]$Source = `" `"hello world`" `";Write-Host $Source;" < nul
Is there a way to do what I want? You might ask why I am doing this, but it’s a long story, so I won’t go into the details.
thanks
You'll have to use a combination of batch's escape character and PowerShell's escape character.
In batch, when escaping quotes, you use the common shell backslash (\) to escape those quotes. In Powershell, you use the backtick `.
So if you wanted to use batch to print out a quoted string with Powershell, you need to first batch escape the quote to declare the variable in Powershell, then to ensure the string is quoted you need batch and Powershell escape another quote, and then your add your desired string, ensuring you batch escape first.
For your example, this will work:
powershell -command "[string]$Source = \"`\"hello world`\"\"; Write-Host $Source;"
Here's a break down of the declaration of the $Source variable:
"[string]$Source = # open quote to begin -command parameter declaration
\" # batch escape to begin the string portion
`\" # Powershell+Batch escape
hello world # Your content
`\" # Posh+Batch again
\"; # Close out the batch and continue
more commands " # Close quote on -command parameter
This renders the string like this in batch:
`"hello world`"
One note, you don't need to explicitly cast $Source as a string since you are building it as a literal string from scratch.
$Source = "string stuff" will work as intended.
I need to have a hotstring with backticks in it (`) surrounding it. Simplifying what I've tried:
::`hw`::Hello, World!
Running it gives an error: "Invalid hotkey".
I'm not sure why this restriction exists but more to the point: is there any workaround?
A backtick (`) is the default escape character in AHK.
In order to specifiy a literal backtick, you can either escape it (with itself):
::``hw``::Hello, World!
Or change the escape character:
#EscapeChar \
::`hw`::Hello, World!
I wouldn't recommend the latter, since many libraries expect the escape char to be a backtick.
Here's my script:
#rasdial "My VPN" "user#domain" 'my<password'
My password contains < character. This, without # works when entered into PowerShell console.
I know ` is the quoting character, but obviously it doesn't work with <.
My password contains many special characters so it has to be quoted. But it doesn't work when double quotes are used. How to escape the password properly? Is there a way to import it from an external file? BTW, rasphone.exe remembers my password, maybe is there a way to use it?
Here's the solution I found working. I used double quotes instead of single quotes. It didn't work at the first time, because there was also % in my real password, which needed to be quoted with %%.
So, inside .cmd script: 'my%`<password' won't work, but "my%%<password" will.
Can anyone confirm he used string like '...`<...' inside .cmd script and it worked? Does it behave differently in different PowerShell versions? Mine is from Windows 8 x64.
I had a similar issue and it took me a while to get to the bottom of it. In my case I had a password similar to this
}K)/]j..|{?*&%($#7}e$%0>;._#
Putting it in double quotes allows for variable expansion so obviously that wont work.
Putting it in single quotes is supposed to work but didn't for me. Escaping problem characters with the back tick also didnt work for me.
What worked for me was to surround with single quotes first and then double quotes
'"}K)/]j..|{?*&%($#7}e$%0>;._#"'
Have you tried:
'my`<password'
` <--- The tilde key backquote is the Powershell escape character.
I would also encourage using -AsSecureString
Testing in the PS console:
PS C:\Users\Athomsfere> $pw = 'my<password'
PS C:\Users\Athomsfere> write-host $pw
my<password
So its not the character by itself.
It still woks like this too: (As a script)
'#rasdial', "My VPN", "user#domain", 'my<password' |`
ForEach
{
Write-Host $_
}
I want to include an apostrophe in my string. Is it possible to do without using double quotes?
'This is a quote. Can`'t I just include a single quote in it?'
'This is another quote that doesn\'t work'
'Escape a single quote '' using a double single quote'
See the help for the quoting rules.
You can check out the help in the powershell command line by typing:
Get-Help about_Quoting_Rules
It explains that backticks are interpreted literally in single-quoted strings.
Because the contents of single-quoted strings are interpreted literally, you cannot use the backtick character to force a literal character interpretation in a single-quoted string.