# Expand a variable in a scriptblock

**URL:** <https://forums.powershell.org/t/expand-a-variable-in-a-scriptblock/12546>\
**Category:** PowerShell Help\
**Created:** [May 12, 2019, 10:27am UTC](https://forums.powershell.org/t/expand-a-variable-in-a-scriptblock/12546 "2019-05-12T10:27:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gvannauw](https://avatars.discourse-cdn.com/v4/letter/g/f9ae1b/32.png) [@gvannauw](https://forums.powershell.org/u/gvannauw)\
**Post date:** [May 12, 2019, 10:27am UTC](https://forums.powershell.org/t/expand-a-variable-in-a-scriptblock/12546/1 "2019-05-12T10:27:06Z")

</div>

Hello All

I am writing a powerscript to display a message on every computerscreen. However I wanted it to be flexible and to take the input from a command line (or a text file).

The moment the variable is used inside the scriptblock, it’s not working anymore

[pre]

function SendAll($msg) {  
$pclist=Get-Content $list  
$number\_of\_pcs=$pclist.count  
write-host “Sending message to” $number\_of\_pcs “systems”  
foreach ($pc in $pclist) {  
write-host "Sending to " $pc  
invoke-command -computername $pc -scriptblock {  
$cmdMessage = {c:\windows\system32\msg.exe \* ‘Please logoff - system restart in 5 minutes’}  
$cmdMessage | invoke-expression  
}  
}  
}  
[/pre]

now, above works fine, but this means altering the text each and every time  
What I was hoping to achieve was to use $msg (which contains the full command, i.e. {c:\windows\system32\msg.exe \* 'text which was input at an earlier stage to send the message, something along following lines

&nbsp;

Invoke-Command -Computername $pc -Scriptblock {  
$msg | invoke-expression

But the variable $msg is not expanding in the scriptblock, therefore I only receive an error message when running the script

Can anyone point me into the right direction on how to solve this ?

Thank you

&nbsp;

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [May 12, 2019, 10:29am UTC](https://forums.powershell.org/t/expand-a-variable-in-a-scriptblock/12546/2 "2019-05-12T10:29:24Z")

</div>

The thing to bear in mind is that you’re running that scriptblock in a new session on another PC - it can’t access the variables you have in your current scope.

You can pass arguments to the scriptblock by adding a param block, and using `-ArgumentList` on Invoke-Command.

For instance:  
[pre]  
param(  
$MSG  
)  
Invoke-Command -ComputerName $Computer -ArgumentList $MSG -ScriptBlock {  
param($Msg)  
msg \* $Msg  
}  
[/pre]

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [May 12, 2019, 2:38pm UTC](https://forums.powershell.org/t/expand-a-variable-in-a-scriptblock/12546/3 "2019-05-12T14:38:15Z")

</div>

In PS v3 and higher, you can also do it a little more directly using the `$using:` prefix:

```
param(
$Msg
)
Invoke-Command -ComputerName $Computer -ScriptBlock {
msg * $using:Msg
}
```

---

<div class="post-metadata">

**Author:** ![gvannauw](https://avatars.discourse-cdn.com/v4/letter/g/f9ae1b/32.png) [@gvannauw](https://forums.powershell.org/u/gvannauw)\
**Post date:** [May 13, 2019, 1:32am UTC](https://forums.powershell.org/t/expand-a-variable-in-a-scriptblock/12546/4 "2019-05-13T01:32:33Z")

</div>

thank you. I will alter the code and give it a try

---

<div class="post-metadata">

**Author:** ![lovelyseraph](https://avatars.discourse-cdn.com/v4/letter/l/7cd45c/32.png) [@lovelyseraph](https://forums.powershell.org/u/lovelyseraph)\
**Post date:** [May 13, 2019, 4:47am UTC](https://forums.powershell.org/t/expand-a-variable-in-a-scriptblock/12546/5 "2019-05-13T04:47:36Z")

</div>

I modify it like this:

```
function Send-Message
{
Param ($list,$msg)
$pclist=Get-Content $list
write-host ("Sending message to ["+$pclist.count+"] systems")
$Sessions = $pclist |%{New-PSSession -ComputerName $_}
invoke-command -Session $Sessions -scriptblock {
	Write-host "Sending to: " $env:computername
	msg.exe * $Using:msg
	}
}

Send-Message -list "d:\list.txt" -msg 'Please logoff – system restart in 5 minutes'
```

Use session faster than foreach.

&nbsp;

---

<div class="post-metadata">

**Author:** ![gvannauw](https://avatars.discourse-cdn.com/v4/letter/g/f9ae1b/32.png) [@gvannauw](https://forums.powershell.org/u/gvannauw)\
**Post date:** [May 13, 2019, 7:59am UTC](https://forums.powershell.org/t/expand-a-variable-in-a-scriptblock/12546/6 "2019-05-13T07:59:40Z")

</div>

thank you all for your kind explanations and examples … It now works perfectly…

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:32pm UTC](https://forums.powershell.org/t/expand-a-variable-in-a-scriptblock/12546/7 "2024-05-16T20:32:51Z")

</div>


