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 - Append string to variable in Function

Reply
 
Old 05-15-2008   #1 (permalink)
Victag


 
 

Append string to variable in Function

Powershell newb here..so used to vbscript...can someone tell me how to
make this work so that I can append text to my string variable within
the function?

$Global:myvar = "testing"
write-host $myvar

Function addstuff()
{
$myvar = $myvar +"123"

}

addstuff
write-host $myvar
Function addstuff()
{
$myvar = $myvar +"123"

}

My System SpecsSystem Spec
Old 05-15-2008   #2 (permalink)
Marco Shaw [MVP]


 
 

Re: Append string to variable in Function

Victag wrote:
Quote:

> Powershell newb here..so used to vbscript...can someone tell me how to
> make this work so that I can append text to my string variable within
> the function?
>
> $Global:myvar = "testing"
> write-host $myvar
>
> Function addstuff()
> {
> $myvar = $myvar +"123"
>
> }
>
> addstuff
> write-host $myvar
> Function addstuff()
> {
> $myvar = $myvar +"123"
>
> }
Well, you first declare your variable globally, so within your function,
you must do the same if you want to refer to the same:

function addstuff()
{
$global:myvar=$global:myvar + "123"
}

When you also echo it out, you'll need to do
$global:myvar
-or-
write-host $global:myvar

Marco

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 05-15-2008   #3 (permalink)
Victag


 
 

Re: Append string to variable in Function

On May 15, 12:08*pm, "Marco Shaw [MVP]"
<marco.shaw@_NO_SPAM_gmail.com> wrote:
Quote:

> Victag wrote:
Quote:

> > Powershell newb here..so used to vbscript...can someone tell me how to
> > make this work so that I can append text to my string variable within
> > the function?
>
Quote:

> > $Global:myvar = "testing"
> > write-host $myvar
>
Quote:

> > Function addstuff()
> > {
> > *$myvar = $myvar +"123"
>
Quote:

> > }
>
Quote:

> > addstuff
> > write-host $myvar
> > Function addstuff()
> > {
> > *$myvar = $myvar +"123"
>
Quote:

> > }
>
> Well, you first declare your variable globally, so within your function,
> you must do the same if you want to refer to the same:
>
> function addstuff()
> {
> * *$global:myvar=$global:myvar + "123"
>
> }
>
> When you also echo it out, you'll need to do
> $global:myvar
> -or-
> write-host $global:myvar
>
> Marco
>
> --
> Microsoft MVP - Windows PowerShellhttp://www.microsoft.com/mvp
>
> PowerGadgets MVPhttp://www.powergadgets.com/mvp
>
> Blog:http://marcoshaw.blogspot.com- Hide quoted text -
>
> - Show quoted text -
Worked like a charm...THANKS!
My System SpecsSystem Spec
Old 05-16-2008   #4 (permalink)
Alex K. Angelopoulos


 
 

Re: Append string to variable in Function

FYI, you can also shorthand it like this:

$global:myvar += "123"




"Victag" <miketaggart@xxxxxx> wrote in message
news:d857aa40-d1fa-4a1d-aee5-f314c78e167d@xxxxxx
Quote:

> On May 15, 12:08 pm, "Marco Shaw [MVP]"
> <marco.shaw@_NO_SPAM_gmail.com> wrote:
Quote:

>> Victag wrote:
Quote:

>> > Powershell newb here..so used to vbscript...can someone tell me how to
>> > make this work so that I can append text to my string variable within
>> > the function?
>>
Quote:

>> > $Global:myvar = "testing"
>> > write-host $myvar
>>
Quote:

>> > Function addstuff()
>> > {
>> > $myvar = $myvar +"123"
>>
Quote:

>> > }
>>
Quote:

>> > addstuff
>> > write-host $myvar
>> > Function addstuff()
>> > {
>> > $myvar = $myvar +"123"
>>
Quote:

>> > }
>>
>> Well, you first declare your variable globally, so within your function,
>> you must do the same if you want to refer to the same:
>>
>> function addstuff()
>> {
>> $global:myvar=$global:myvar + "123"
>>
>> }
>>
>> When you also echo it out, you'll need to do
>> $global:myvar
>> -or-
>> write-host $global:myvar
>>
>> Marco
>>
>> --
>> Microsoft MVP - Windows PowerShellhttp://www.microsoft.com/mvp
>>
>> PowerGadgets MVPhttp://www.powergadgets.com/mvp
>>
>> Blog:http://marcoshaw.blogspot.com- Hide quoted text -
>>
>> - Show quoted text -
>
> Worked like a charm...THANKS!
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
variable evaluation in a string PowerShell
Find a string within a variable string PowerShell
SSIS - string variable! .NET General
Subject: using a Variable with/in a string PowerShell
Using group-object with a variable string 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