# Script Blocks and Invoke-Command

**URL:** <https://forums.powershell.org/t/script-blocks-and-invoke-command/8002>\
**Category:** PowerShell Help\
**Created:** [February 6, 2017, 6:43pm UTC](https://forums.powershell.org/t/script-blocks-and-invoke-command/8002 "2017-02-06T18:43:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![techtim2003yahoo-com](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@techtim2003yahoo-com](https://forums.powershell.org/u/techtim2003yahoo-com)\
**Post date:** [February 6, 2017, 6:43pm UTC](https://forums.powershell.org/t/script-blocks-and-invoke-command/8002/1 "2017-02-06T18:43:43Z")

</div>

Am a bit confused as to why I am seeing the behavior I am seeing. If anyone can point me to some reading if it is not an easy explanation that would be great.

Simple script block.

```
$sb = {
$DefaultFolder = "Test2"
$DefaultPath = "C:\temp\FolderTest\"
$date = Get-Date -format MM-yyyy
$newname = $env:COMPUTERNAME + '-' + $date
$folders = Get-ChildItem -Name
Foreach ($folder in $folders){
IF ($folder.Contains("$DefaultFolder") -eq $True ) {
$final = Join-Path $DefaultPath $DefaultFolder
Rename-Item $final $newname}
}
}
```

If I execute it like:

```
& $sb
```

Then it works fine on the local box. However, if I try to send it down an invoke-command (even to the same local system) either by doing computername or on a session nothing happens.

```
Invoke-command -computername $hostname -scriptblock $sb
```

or

```
$s = new-pssession -computername $hostname -crediential $cred
Invoke-command -Session $s -scriptblock $sb
```

But If i do something simple like:

```
Invoke-command -computername $hostname -scriptblock {get-process}
```

It works fine.

What am I missing?

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [February 7, 2017, 5:30am UTC](https://forums.powershell.org/t/script-blocks-and-invoke-command/8002/2 "2017-02-07T05:30:27Z")

</div>

I think below line is missing the parameter -Path or -LiteralPath. Get-ChildItem assumes the current working directory of your session if you don’t specify -Path or -LiteralPath.

```
$folders = Get-ChildItem -Name
```

If you replace with below it should work:

```
$folders = Get-ChildItem -Name -Path $DefaultPath
```

---

<div class="post-metadata">

**Author:** ![techtim2003yahoo-com](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@techtim2003yahoo-com](https://forums.powershell.org/u/techtim2003yahoo-com)\
**Post date:** [February 7, 2017, 1:06pm UTC](https://forums.powershell.org/t/script-blocks-and-invoke-command/8002/3 "2017-02-07T13:06:11Z")

</div>

That was totally it. Hence why it was working locally because I was sitting in the path inside the shell.

Thanks!

---

<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:39pm UTC](https://forums.powershell.org/t/script-blocks-and-invoke-command/8002/4 "2024-05-16T20:39:18Z")

</div>

Closed on May 16, 2024, 8:39pm UTC


