![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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" > > } 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 Specs![]() |
| | #3 (permalink) |
| | 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 - |
My System Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||