Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Concatenate variables with numbers

Reply
 
Old 03-05-2008   #1 (permalink)
Orrin


 
 

Concatenate variables with numbers

Hello everyone,

I'm having some issues getting a variable valued the way I want it.
I'm using the get-date cmdlet to get the current date from the system,
I'm only concerned with the year and month, but I want to set the
variable to last month.

In the case of March 2008, I can easily set my variable to February
2008 like this:

(get-date -uformat "%Y%m") -1

The problem is that if it were to be January 2008, using the above, my
variable would be valued to this:

200800

Shouldn't it know that this is a date since I directly used the get-
date cmdlet?

So, I've tried making the value returned from get-date a string like
this:

((get-date).toString('yyyy'))-1

The only problem is that when I go to concatenate it with a month
value, it always adds the two numeric values so instead of getting a
variable valued like this:

200712

I get one valued like this:

2019

I have searched and can not find a way to concatenate the value of the
strings without adding them. I've tried the following:

$YM = $MoveYear + $MoveMonth
$YM = $MoveYear += $MoveMonth

Both give the same result.

Thank you.

My System SpecsSystem Spec
Old 03-05-2008   #2 (permalink)
Jon


 
 

Re: Concatenate variables with numbers

This may be simpler ...

(get-date "1/1/2008").AddMonths(-1).Month
(get-date "1/1/2008").AddMonths(-1).Year

--
Jon


"Orrin" <oedenfield@xxxxxx> wrote in message
news:96e8203d-4371-4428-87bb-ae573875f9a2@xxxxxx
Quote:

> Hello everyone,
>
> I'm having some issues getting a variable valued the way I want it.
> I'm using the get-date cmdlet to get the current date from the system,
> I'm only concerned with the year and month, but I want to set the
> variable to last month.
>
> In the case of March 2008, I can easily set my variable to February
> 2008 like this:
>
> (get-date -uformat "%Y%m") -1
>
> The problem is that if it were to be January 2008, using the above, my
> variable would be valued to this:
>
> 200800
>
> Shouldn't it know that this is a date since I directly used the get-
> date cmdlet?
>
> So, I've tried making the value returned from get-date a string like
> this:
>
> ((get-date).toString('yyyy'))-1
>
> The only problem is that when I go to concatenate it with a month
> value, it always adds the two numeric values so instead of getting a
> variable valued like this:
>
> 200712
>
> I get one valued like this:
>
> 2019
>
> I have searched and can not find a way to concatenate the value of the
> strings without adding them. I've tried the following:
>
> $YM = $MoveYear + $MoveMonth
> $YM = $MoveYear += $MoveMonth
>
> Both give the same result.
>
> Thank you.
My System SpecsSystem Spec
Old 03-05-2008   #3 (permalink)
Orrin


 
 

Re: Concatenate variables with numbers

Thank you for your help.

Using this method to set the dates of the variables still yield me two
variables that when I concatenate them I get 2019.

Also will this method work when get-date returns a date like
01/01/2010 ? I should have clarified; I am using this in a script
that I want to have setup so that no matter what the year is, if get-
date returns a MM of 01, then it will be able to set a variable as
yyyyMM (December of the previous year). For some reason when
Powershell is returning a DATE is not letting me subtract 1 month and
get December of the previous year (it will easily show a month of 00
instead).

I just tried:

(get-date).AddMonths(-1) -uformat "%Y%m"
and
(get-date -uformat "%Y%m").AddMonths(-1)

and they returned errors.

I must be missing something.

Orrin


On Mar 5, 2:21 pm, "Jon" <Email_Addr...@xxxxxx> wrote:
Quote:

> This may be simpler ...
>
> (get-date "1/1/2008").AddMonths(-1).Month
> (get-date "1/1/2008").AddMonths(-1).Year
>
> --
> Jon
>
> "Orrin" <oedenfi...@xxxxxx> wrote in message
>
> news:96e8203d-4371-4428-87bb-ae573875f9a2@xxxxxx
>
Quote:

> > Hello everyone,
>
Quote:

> > I'm having some issues getting a variable valued the way I want it.
> > I'm using the get-date cmdlet to get the current date from the system,
> > I'm only concerned with the year and month, but I want to set the
> > variable to last month.
>
Quote:

> > In the case of March 2008, I can easily set my variable to February
> > 2008 like this:
>
Quote:

> > (get-date -uformat "%Y%m") -1
>
Quote:

> > The problem is that if it were to be January 2008, using the above, my
> > variable would be valued to this:
>
Quote:

> > 200800
>
Quote:

> > Shouldn't it know that this is a date since I directly used the get-
> > date cmdlet?
>
Quote:

> > So, I've tried making the value returned from get-date a string like
> > this:
>
Quote:

> > ((get-date).toString('yyyy'))-1
>
Quote:

> > The only problem is that when I go to concatenate it with a month
> > value, it always adds the two numeric values so instead of getting a
> > variable valued like this:
>
Quote:

> > 200712
>
Quote:

> > I get one valued like this:
>
Quote:

> > 2019
>
Quote:

> > I have searched and can not find a way to concatenate the value of the
> > strings without adding them. I've tried the following:
>
Quote:

> > $YM = $MoveYear + $MoveMonth
> > $YM = $MoveYear += $MoveMonth
>
Quote:

> > Both give the same result.
>
Quote:

> > Thank you.
My System SpecsSystem Spec
Old 03-05-2008   #4 (permalink)
Shay Levi


 
 

Re: Concatenate variables with numbers


Hi Orrin,

It doesn't work because the -uformat is a parameter for get-date and for
AddMonths() method.

Try this instead:

PS > (get-date).AddMonths(-1).ToString("yyyyMM")
200802


Another way to get it is with the .NET string format operator:

PS > "{0:yyyyMM}" -f (get-date).AddMonths(-1)
200802



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Quote:

> Thank you for your help.
>
> Using this method to set the dates of the variables still yield me two
> variables that when I concatenate them I get 2019.
>
> Also will this method work when get-date returns a date like
> 01/01/2010 ? I should have clarified; I am using this in a script
> that I want to have setup so that no matter what the year is, if get-
> date returns a MM of 01, then it will be able to set a variable as
> yyyyMM (December of the previous year). For some reason when
> Powershell is returning a DATE is not letting me subtract 1 month and
> get December of the previous year (it will easily show a month of 00
> instead).
>
> I just tried:
>
> (get-date).AddMonths(-1) -uformat "%Y%m"
> and
> (get-date -uformat "%Y%m").AddMonths(-1)
> and they returned errors.
>
> I must be missing something.
>
> Orrin
>
> On Mar 5, 2:21 pm, "Jon" <Email_Addr...@xxxxxx> wrote:
>
Quote:

>> This may be simpler ...
>>
>> (get-date "1/1/2008").AddMonths(-1).Month
>> (get-date "1/1/2008").AddMonths(-1).Year
>> --
>> Jon
>> "Orrin" <oedenfi...@xxxxxx> wrote in message
>>
>> news:96e8203d-4371-4428-87bb-ae573875f9a2@xxxxxx
>> ..
>>
Quote:

>>> Hello everyone,
>>>
>>> I'm having some issues getting a variable valued the way I want it.
>>> I'm using the get-date cmdlet to get the current date from the
>>> system, I'm only concerned with the year and month, but I want to
>>> set the variable to last month.
>>>
>>> In the case of March 2008, I can easily set my variable to February
>>> 2008 like this:
>>>
>>> (get-date -uformat "%Y%m") -1
>>>
>>> The problem is that if it were to be January 2008, using the above,
>>> my variable would be valued to this:
>>>
>>> 200800
>>>
>>> Shouldn't it know that this is a date since I directly used the get-
>>> date cmdlet?
>>>
>>> So, I've tried making the value returned from get-date a string like
>>> this:
>>>
>>> ((get-date).toString('yyyy'))-1
>>>
>>> The only problem is that when I go to concatenate it with a month
>>> value, it always adds the two numeric values so instead of getting a
>>> variable valued like this:
>>>
>>> 200712
>>>
>>> I get one valued like this:
>>>
>>> 2019
>>>
>>> I have searched and can not find a way to concatenate the value of
>>> the strings without adding them. I've tried the following:
>>>
>>> $YM = $MoveYear + $MoveMonth
>>> $YM = $MoveYear += $MoveMonth
>>> Both give the same result.
>>>
>>> Thank you.
>>>

My System SpecsSystem Spec
Old 03-05-2008   #5 (permalink)
Jon


 
 

Re: Concatenate variables with numbers

"Orrin" <oedenfield@xxxxxx> wrote in message
news:67b12cc6-fc47-47bd-a196-03d9086138ec@xxxxxx
Quote:

> Thank you for your help.
>
> Using this method to set the dates of the variables still yield me two
> variables that when I concatenate them I get 2019.
>

Shay's given you the solution, to which I'd only add that the reason you got
2019 was that you were adding 2 integers 2007 and 12. To concatenate, you'd
cast to strings eg

eg compare these two

2007 + 12
[string]2007 + [string]12

--
Jon

My System SpecsSystem Spec
Old 03-05-2008   #6 (permalink)
Orrin


 
 

Re: Concatenate variables with numbers

That is it! Thank you so much! I knew there had to be a simple way
to to do it.

Orrin

On Mar 5, 3:36 pm, Shay Levi <n...@xxxxxx> wrote:
Quote:

> Hi Orrin,
>
> It doesn't work because the -uformat is a parameter for get-date and for
> AddMonths() method.
>
> Try this instead:
>
> PS > (get-date).AddMonths(-1).ToString("yyyyMM")
> 200802
>
> Another way to get it is with the .NET string format operator:
>
> PS > "{0:yyyyMM}" -f (get-date).AddMonths(-1)
> 200802
>
> -----
> Shay Levi
> $cript Fanatichttp://scriptolog.blogspot.com
>
Quote:

> > Thank you for your help.
>
Quote:

> > Using this method to set the dates of the variables still yield me two
> > variables that when I concatenate them I get 2019.
>
Quote:

> > Also will this method work when get-date returns a date like
> > 01/01/2010 ? I should have clarified; I am using this in a script
> > that I want to have setup so that no matter what the year is, if get-
> > date returns a MM of 01, then it will be able to set a variable as
> > yyyyMM (December of the previous year). For some reason when
> > Powershell is returning a DATE is not letting me subtract 1 month and
> > get December of the previous year (it will easily show a month of 00
> > instead).
>
Quote:

> > I just tried:
>
Quote:

> > (get-date).AddMonths(-1) -uformat "%Y%m"
> > and
> > (get-date -uformat "%Y%m").AddMonths(-1)
> > and they returned errors.
>
Quote:

> > I must be missing something.
>
Quote:

> > Orrin
>
Quote:

> > On Mar 5, 2:21 pm, "Jon" <Email_Addr...@xxxxxx> wrote:
>
Quote:
Quote:

> >> This may be simpler ...
>
Quote:
Quote:

> >> (get-date "1/1/2008").AddMonths(-1).Month
> >> (get-date "1/1/2008").AddMonths(-1).Year
> >> --
> >> Jon
> >> "Orrin" <oedenfi...@xxxxxx> wrote in message
>
Quote:
Quote:

> >>news:96e8203d-4371-4428-87bb-ae573875f9a2@xxxxxx
> >> ..
>
Quote:
Quote:

> >>> Hello everyone,
>
Quote:
Quote:

> >>> I'm having some issues getting a variable valued the way I want it.
> >>> I'm using the get-date cmdlet to get the current date from the
> >>> system, I'm only concerned with the year and month, but I want to
> >>> set the variable to last month.
>
Quote:
Quote:

> >>> In the case of March 2008, I can easily set my variable to February
> >>> 2008 like this:
>
Quote:
Quote:

> >>> (get-date -uformat "%Y%m") -1
>
Quote:
Quote:

> >>> The problem is that if it were to be January 2008, using the above,
> >>> my variable would be valued to this:
>
Quote:
Quote:

> >>> 200800
>
Quote:
Quote:

> >>> Shouldn't it know that this is a date since I directly used the get-
> >>> date cmdlet?
>
Quote:
Quote:

> >>> So, I've tried making the value returned from get-date a string like
> >>> this:
>
Quote:
Quote:

> >>> ((get-date).toString('yyyy'))-1
>
Quote:
Quote:

> >>> The only problem is that when I go to concatenate it with a month
> >>> value, it always adds the two numeric values so instead of getting a
> >>> variable valued like this:
>
Quote:
Quote:

> >>> 200712
>
Quote:
Quote:

> >>> I get one valued like this:
>
Quote:
Quote:

> >>> 2019
>
Quote:
Quote:

> >>> I have searched and can not find a way to concatenate the value of
> >>> the strings without adding them. I've tried the following:
>
Quote:
Quote:

> >>> $YM = $MoveYear + $MoveMonth
> >>> $YM = $MoveYear += $MoveMonth
> >>> Both give the same result.
>
Quote:
Quote:

> >>> Thank you.
My System SpecsSystem Spec
Old 03-05-2008   #7 (permalink)
Orrin


 
 

Re: Concatenate variables with numbers

Thank you for your help Jon. I really appreciate it.

Orrin

On Mar 5, 3:59 pm, "Jon" <Email_Addr...@xxxxxx> wrote:
Quote:

> Shay's given you the solution, to which I'd only add that the reason you got
> 2019 was that you were adding 2 integers 2007 and 12. To concatenate, you'd
> cast to strings eg
>
> eg compare these two
>
> 2007 + 12
> [string]2007 + [string]12
>
> --
> Jon
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
math with "GB/MB/KB" in variables fails, without variables works? PowerShell
How to concatenate a literal string and a variable value? VB Script
How to Concatenate text files? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46