Powershell any way to hide yes and no popup while adding registry [duplicate] - powershell

Powershell seems to drop empty string arguments when passed to a command. I have this code
PS D:\> $b.name = "foo bar"
PS D:\> ./echoargs $b.name
Arg 0 is D:\echoargs.exe
Arg 1 is foo bar
PS D:\> $b.name = ""
PS D:\> ./echoargs $b.name
Arg 0 is D:\echoargs.exe
You can assume that $b has a 'name' member. How can i pass this as an argument to the exe even when the value is an empty string. I've tried using the call operator with no success.

If you want an empty string to appear try escaped quotes around the argument like so:
PS> $b = [psobject]#{name = ''}
PS> echoargs `"$($b.Name)`"
Arg 0 is <>
Command line:
"C:\Users\Keith\Pscx\Trunk\Src\Pscx\bin\Release\Apps\EchoArgs.exe" ""
Note that I tested this on V3 so I'm not sure if the behavior will be exactly the same on V2.

Try to pass an empty single quote string enclosed in double quotes, or vice versa.
./echoargs $b.name, "''"
or
./echoargs $b.name, '""'

This should also work:
./echoargs [String]::Empty

Related

Import Certificate using Powershell script: The specified network password is not correct [duplicate]

Simple questions that's been bugging me: In powershell, I can define strings like so:
$s1 = "Boogety boo"
or
$s2 = '.net rocks'
Is there a difference to the interpreter?
Double quotes allow variable expansion while single quotes do not:
PS C:\Users\Administrator> $mycolor="red"
PS C:\Users\Administrator> write-output -inputobject 'My favorite color is $mycolor'
My favorite color is $mycolor
Source: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
(I know version 1.0 but the principle is still the same)
This is not trying to be a better answer. Just another way to say it.
The variable expansion between apostrophes and quotes are the same as on UNIX shells (sh, ksh, bash). Using apostrophes will take the character string as-is, without processing any escapes.
PS C:\Users\lit> $x = "`t"
PS C:\Users\lit> $x
PS C:\Users\lit> Write-Output "now${x}is"
now is
PS C:\Users\lit> $x = '`t'
PS C:\Users\lit> $x
`t
PS C:\Users\lit> Write-Output "now${x}is"
now`tis
PS C:\Users\lit> $word = "easy"
PS C:\Users\lit> "PowerShell is $word"
PowerShell is easy
PS C:\Users\lit> 'PowerShell is $word'
PowerShell is $word
This question has a direct answer in the about_Quoting_Rules article of the PowerShell docs:
Double-quoted strings
A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.
For example:
$i = 5
"The value of $i is $i."
The output of this command is:
The value of 5 is 5.
Single-quoted strings
A string enclosed in single-quotation marks is a verbatim string. The string is passed to the command exactly as you type it. No substitution is performed. For example:
$i = 5
'The value of $i is $i.'
The output of this command is:
The value of $i is $i.
In other words, use single quotes if you want your string to remain as written. Use double quotes, if you want to insert variables ($myVariable), results of command executions and other evaluations ($($myList -join ', ')) or special characters (`r, `n, `t, `a, etc.).

Powershell strings with double and single quotes [duplicate]

Simple questions that's been bugging me: In powershell, I can define strings like so:
$s1 = "Boogety boo"
or
$s2 = '.net rocks'
Is there a difference to the interpreter?
Double quotes allow variable expansion while single quotes do not:
PS C:\Users\Administrator> $mycolor="red"
PS C:\Users\Administrator> write-output -inputobject 'My favorite color is $mycolor'
My favorite color is $mycolor
Source: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
(I know version 1.0 but the principle is still the same)
This is not trying to be a better answer. Just another way to say it.
The variable expansion between apostrophes and quotes are the same as on UNIX shells (sh, ksh, bash). Using apostrophes will take the character string as-is, without processing any escapes.
PS C:\Users\lit> $x = "`t"
PS C:\Users\lit> $x
PS C:\Users\lit> Write-Output "now${x}is"
now is
PS C:\Users\lit> $x = '`t'
PS C:\Users\lit> $x
`t
PS C:\Users\lit> Write-Output "now${x}is"
now`tis
PS C:\Users\lit> $word = "easy"
PS C:\Users\lit> "PowerShell is $word"
PowerShell is easy
PS C:\Users\lit> 'PowerShell is $word'
PowerShell is $word
This question has a direct answer in the about_Quoting_Rules article of the PowerShell docs:
Double-quoted strings
A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.
For example:
$i = 5
"The value of $i is $i."
The output of this command is:
The value of 5 is 5.
Single-quoted strings
A string enclosed in single-quotation marks is a verbatim string. The string is passed to the command exactly as you type it. No substitution is performed. For example:
$i = 5
'The value of $i is $i.'
The output of this command is:
The value of $i is $i.
In other words, use single quotes if you want your string to remain as written. Use double quotes, if you want to insert variables ($myVariable), results of command executions and other evaluations ($($myList -join ', ')) or special characters (`r, `n, `t, `a, etc.).

How to correctly escape spaces and backslashes in command line arguments?

I had some issues passing an array of strings to a command in PowerShell, so I'm debugging my script. I'm using the EchoArgs.exe program found in the PowerShell Community Extension Project (PSCX).
If I execute this script:
Import-Module Pscx
cls
$thisOne = 'this_one\';
$secondOne = 'second one\';
$lastOne = 'last_one'
$args = $thisOne `
, "the $secondOne" `
, "the_$lastOne"
EchoArgs $args
I get this result:
Arg 0 is <this_one\>
Arg 1 is <the second one" the_last_one>
Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" this_one\ "the second one\" the_last_one
It seems that if a string contains spaces, the last backslash escapes the double quote. In fact all seems working if I escape only that backslash:
Import-Module Pscx
cls
$thisOne = 'this_one\';
$secondOne = 'second one\\';
$lastOne = 'last_one'
$args = $thisOne `
, "the $secondOne" `
, "the_$lastOne"
EchoArgs $args
with this result:
Arg 0 is <this_one\>
Arg 1 is <the second one\>
Arg 2 is <the_last_one>
Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" this_one\ "the second one\\" the_last_one
Is there a "smart" way in PowerShell (i.e. a cmdlet) to escape any string in order to avoid such issues?
Try using Start-Process instead. It has an $Arguments parameter that would suit this better.
See here: PowerShell - Start-Process and Cmdline Switches

Spaced paths, msbuild, and psake

Related question here.
This works properly for compiling an mvc3 application.
task Compile
{
$config = $script:siteConfig.config
exec { & "C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe" $webproject_path `
/p:Configuration=$config /p:WebProjectOutputDir="$publish_dir" `
/p:Outdir="$out_dir" /p:CleanWebProjectOutputDir=False `
/T:_WPPCopyWebApplication /T:ResolveReferences /verbosity:quiet /nologo }
}
All of those path variables are script properties. However, when spaces are introduced in those calculated paths (e.g. the project is moved from C:\Projects\ to C:\Users\ASDFG1\Documents\Visual Studio 2010\Projects) msbuild thinks there's multiple project files. This makes sense but I have to be missing something, getting a parsed variable into quotes shouldn't be this hard.
Variations tried
exec { Invoke-Expression "& C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe '$webproject_path' /p:Configuration=$config /p:WebProjectOutputDir='$publish_dir' /p:Outdir='$out_dir' /p:CleanWebProjectOutputDir=False /T:_WPPCopyWebApplication /T:ResolveReferences /verbosity:quiet /nologo" }
exec { C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe "`"$webproject_path`"" `
/p:Configuration=$config /p:WebProjectOutputDir="`"$publish_dir`"" `
/p:Outdir="`"$out_dir`"" /p:CleanWebProjectOutputDir=False `
/T:_WPPCopyWebApplication /T:ResolveReferences /verbosity:quiet /nologo }
Using EchoArgs.exe to reproduce the problem, we see that quotes are not being passed to the executable as desired:
PS> $publish_dir = 'C:\Users\Documents\Visual Studio 2010\Projects'
PS> ./echoargs /p:WebProjectOutputDir="$publish_dir"
Arg 0 is </p:WebProjectOutputDir=C:\Users\Documents\Visual Studio 2010\Projects>
PS> ./echoargs /p:WebProjectOutputDir="`"$publish_dir`""
Arg 0 is </p:WebProjectOutputDir=C:\Users\Documents\Visual>
Arg 1 is <Studio>
Arg 2 is <2010\Projects>
Using the backslash-escaping option from this answer, we can preserve the variable expansion and the enclosing quotes:
PS> ./echoargs /p:WebProjectOutputDir=\`"$publish_dir\`"
Arg 0 is </p:WebProjectOutputDir="C:\Users\Documents\Visual Studio 2010\Projects">
Here, the backticks tell PowerShell to treat the quote characters as literal values, and the backslash tells the call invocation to preserve the quotes.
Alternatively, we could stick with a single level of escaping by evaluating the full argument beforehand, instead of inlining the $publish_dir variable:
PS> $publishArg = '/p:WebProjectOutputDir=\"{0}\"' -f $publish_dir
PS> ./echoargs $publishArg
Arg 0 is </p:WebProjectOutputDir="C:\Users\Documents\Visual Studio 2010\Projects">

What's the difference between single quote and double quote to define a string in powershell

Simple questions that's been bugging me: In powershell, I can define strings like so:
$s1 = "Boogety boo"
or
$s2 = '.net rocks'
Is there a difference to the interpreter?
Double quotes allow variable expansion while single quotes do not:
PS C:\Users\Administrator> $mycolor="red"
PS C:\Users\Administrator> write-output -inputobject 'My favorite color is $mycolor'
My favorite color is $mycolor
Source: http://www.techotopia.com/index.php/Windows_PowerShell_1.0_String_Quoting_and_Escape_Sequences
(I know version 1.0 but the principle is still the same)
This is not trying to be a better answer. Just another way to say it.
The variable expansion between apostrophes and quotes are the same as on UNIX shells (sh, ksh, bash). Using apostrophes will take the character string as-is, without processing any escapes.
PS C:\Users\lit> $x = "`t"
PS C:\Users\lit> $x
PS C:\Users\lit> Write-Output "now${x}is"
now is
PS C:\Users\lit> $x = '`t'
PS C:\Users\lit> $x
`t
PS C:\Users\lit> Write-Output "now${x}is"
now`tis
PS C:\Users\lit> $word = "easy"
PS C:\Users\lit> "PowerShell is $word"
PowerShell is easy
PS C:\Users\lit> 'PowerShell is $word'
PowerShell is $word
This question has a direct answer in the about_Quoting_Rules article of the PowerShell docs:
Double-quoted strings
A string enclosed in double quotation marks is an expandable string. Variable names preceded by a dollar sign ($) are replaced with the variable's value before the string is passed to the command for processing.
For example:
$i = 5
"The value of $i is $i."
The output of this command is:
The value of 5 is 5.
Single-quoted strings
A string enclosed in single-quotation marks is a verbatim string. The string is passed to the command exactly as you type it. No substitution is performed. For example:
$i = 5
'The value of $i is $i.'
The output of this command is:
The value of $i is $i.
In other words, use single quotes if you want your string to remain as written. Use double quotes, if you want to insert variables ($myVariable), results of command executions and other evaluations ($($myList -join ', ')) or special characters (`r, `n, `t, `a, etc.).