Rajiv
Rajiv

Reputation: 685

Add inverted quotes to a value of a variable in PowerShell 2.0

I would like to have a variable value as "1/14/2013 10:00:00" (including the inverted quotes). Below is the code:

$a = Get-Date
$b = $a.AddDays(-1)
$b = $b.ToShortDateString();
$StartTime = "10:00:00 PM"
$EndTime = "11:00:00 PM"
$before = $b + " " + $StartTime
$after = $b + " " + $EndTime

Upon further research, I found that there is an issue reported by Microsoft. It can be found here

Any idea? Thanks!

Upvotes: 2

Views: 14853

Answers (2)

Keith Hill
Keith Hill

Reputation: 202092

If you still need to use variables in your string you can use escaped quotes e.g.:

PS> $before = "`"$b $EndTime`""
PS> $before
"1/14/2013 11:00:00 PM"

Upvotes: 7

Frode F.
Frode F.

Reputation: 54981

If you need to include doubleqoutes in your string, wrap the string with single quotes ' '. If you need single questions, you wrap string with doublequotes. Ex.:

$StartTime = '"10:00:00 PM"'

Output:

[13:00:38] PS-ADMIN > $StartTime
"10:00:00 PM"

Upvotes: 0

Related Questions