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?
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"