ssvegeta96
ssvegeta96

Reputation: 13

How to add timestamp to variable in Powershell

I have a variable in Powershell

$cvm = "derp1"

my question is how do I appened date to it while assigning a new variable.

$name = $(cvm{get-date})

Upvotes: 1

Views: 3883

Answers (2)

Drew
Drew

Reputation: 4030

Good question Vegeta,

What you are after is the + operator which will combine two string.

$cvm = "derp1"
$name = $cvm + (Get-Date)
derp102/06/2019 08:51:53

If you want to add a space, you would do $name = $cvm + " " + (Get-Date) Different formats can be found at https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date?view=powershell-6

Combining two strings is fine, combining an integer and a string will convert the entire lot to a string.

E.G.

$int = 1 # Int32
$string = "hello" # String
$newVar = $int + $string # String

Upvotes: 1

sjdm
sjdm

Reputation: 589

You can convert the date into a string of your chosen date format, and then concatenate the strings :)

$cvm = "derp1"
$name = $cvm + "-" + (get-date).ToString("d")
>> derp1-2/6/2019

"d" in this case is short date, you can choose whatever you like

Upvotes: 0

Related Questions