1

I'm rather new to Powershell scripting and I'm having trouble concatenating cmdlets together into a string. I'm using Powershell V1.0 (due to company restrictions..)

I'm trying to concatenate the day, month and year together without any separators to produce a result like 07082014. I've started by using:

$strDate = get-date -Format "dd" + get-date -Format "MM" + get-date -Format "yyyy"

This produces the error:

Cannot bind parameter because parameter 'Format' is specified more than once. To provide multiple values to parameters that can accept multiple values, use the array syntax. For example, "-parameter value1,value2,value3". System.Management.Automation.CommandNotFoundException: The term 'foobar' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.

So instead I've tried:

$strDay = get-date -Format "dd"
$strDay = $strDay + get-date -Format "MM"
$strDay = $strDay + get-date -Format "yyyy"

But this results in another error:

You must provide a value expression on the right-hand side of the '+' operator.

My understanding is that by using the -Format parameter for get-date, a string value will be returned thus allowing concatenation to another string.

Could anyone help with where I'm going wrong?

1 Answer 1

3

You need brackets around each call to Get-Date so that it knows they are separate:

$strDate = (Get-Date -Format "dd") + (Get-Date -Format "MM") + (Get-Date -Format "yyyy")

Although if you're just trying to get the date in this format, all you really need to do is:

$strDate = Get-Date -Format "ddMMyyyy"
Sign up to request clarification or add additional context in comments.

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.