# icm New-Item Not Creating Subfolder

**URL:** <https://forums.powershell.org/t/icm-new-item-not-creating-subfolder/6726>\
**Category:** PowerShell Help\
**Created:** [June 28, 2016, 2:50pm UTC](https://forums.powershell.org/t/icm-new-item-not-creating-subfolder/6726 "2016-06-28T14:50:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaron-hardy](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@aaron-hardy](https://forums.powershell.org/u/aaron-hardy)\
**Post date:** [June 28, 2016, 2:50pm UTC](https://forums.powershell.org/t/icm-new-item-not-creating-subfolder/6726/1 "2016-06-28T14:50:13Z")

</div>

Likely a simple fix; for some reason the first icm works fine, but the second one does not do anything. It’s as though Test-Path is not including the subdirectory specified in $c, though it does contain a string value.

In a nutshell:

1. Loop through list of remote computers in text file.
2. Check if given remote computer is online.
3. If remote computer online, icm on remote computer to create C:\folder if it does not already exist (this works).
4. icm on backup server (hardcoded name) to create subfolder X:\backups\folder\<strong\>ComputerName if does not already exist (this does not work).
5. Else, return message.

```
foreach ($c in $Computers) {
		
		if ((Test-Connection -ComputerName "$c" -Quiet -Count 1))
		{	
			Invoke-Command -ComputerName $c -Command {
				if (-not (Test-Path C:\folder))
				{
# This works on remote server ($c).
					New-Item -Path C:\folder -ItemType Directory
				}
			};
			
# This does not work on backup server. Permissions are correct. $c is assigned as the subfolder name.
			Invoke-Command -ComputerName "BACKUPSERVER" -Command {
				if (-not (Test-Path ("X:\Backups\folder\$c")))
				{
					New-Item -Path "X:\Backups\folder" -Name "$c" -ItemType Directory -Force
				}
			}
        }
        else
        {
            Write-Warning "Unable to establish connection with system $c ($Date)"
            Write-Output ''
        }
    }
```

Would appreciate some insight to this.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [June 28, 2016, 3:00pm UTC](https://forums.powershell.org/t/icm-new-item-not-creating-subfolder/6726/2 "2016-06-28T15:00:57Z")

</div>

Variable $c has no meaning on the remote machine. You’ve defined it locally. Either use $using:c, or look into the -ArgumentList parameter of Invoke-Command.

See [https://devopscollective.gitbooks.io/the-big-book-of-powershell-gotchas/content/manuscript/remote-variables.html](https://devopscollective.gitbooks.io/the-big-book-of-powershell-gotchas/content/manuscript/remote-variables.html)

---

<div class="post-metadata">

**Author:** ![aaron-hardy](https://avatars.discourse-cdn.com/v4/letter/a/c4cdca/32.png) [@aaron-hardy](https://forums.powershell.org/u/aaron-hardy)\
**Post date:** [June 28, 2016, 3:28pm UTC](https://forums.powershell.org/t/icm-new-item-not-creating-subfolder/6726/3 "2016-06-28T15:28:47Z")

</div>

Thanks Don!

I knew better and somehow kept overlooking it. Helps to take a break sometimes.

---

<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:41pm UTC](https://forums.powershell.org/t/icm-new-item-not-creating-subfolder/6726/4 "2024-05-16T20:41:00Z")

</div>


