1

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})

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the quick tip. I had done some googling prior but if I'm asking this. the other questions I found where way over my head.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.