# Using computername within Invoke-command

**URL:** <https://forums.powershell.org/t/using-computername-within-invoke-command/5952>\
**Category:** PowerShell Help\
**Created:** [March 8, 2016, 8:02am UTC](https://forums.powershell.org/t/using-computername-within-invoke-command/5952 "2016-03-08T08:02:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cory-cameron](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@cory-cameron](https://forums.powershell.org/u/cory-cameron)\
**Post date:** [March 8, 2016, 8:02am UTC](https://forums.powershell.org/t/using-computername-within-invoke-command/5952/1 "2016-03-08T08:02:38Z")

</div>

```
Invoke-Command -Command {robocopy C:\Users \\Server\share\Backup\%computername%\Users /E /ZB} -ComputerName (Get-Content C:\Scripts\Computers.txt) -AsJob -Credential $cred -Authentication CredSSP
```

This creates a folder called %computername% instead of a folder for each computer. How do I correct this and where should I be looking to find this answer? I’m usually pretty good at finding the answer, usually in the PS help, but not sure where to start on this one.

Thanks,

Cory

---

<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:** [March 8, 2016, 8:06am UTC](https://forums.powershell.org/t/using-computername-within-invoke-command/5952/2 "2016-03-08T08:06:30Z")

</div>

%computername% is the old DOS way of getting at environment variables. In PowerShell, it’s $env:computername

```
Invoke-Command -Command {robocopy C:\Users \\Server\share\Backup\$env:computername\Users /E /ZB} -ComputerName (Get-Content C:\Scripts\Computers.txt) -AsJob -Credential $cred -Authentication CredSSP
```

---

<div class="post-metadata">

**Author:** ![stig-sornsen](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@stig-sornsen](https://forums.powershell.org/u/stig-sornsen)\
**Post date:** [March 8, 2016, 8:07am UTC](https://forums.powershell.org/t/using-computername-within-invoke-command/5952/3 "2016-03-08T08:07:24Z")

</div>

Powershell doesn’t understand every command that Cmd does.  
Try to exchange your %computername% with $env:COMPUTERNAME

Best regards  
Stig

Edit: Dave beat me to it 🙂

---

<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:42pm UTC](https://forums.powershell.org/t/using-computername-within-invoke-command/5952/4 "2024-05-16T20:42:41Z")

</div>

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


