# Variable in a start process command

**URL:** <https://forums.powershell.org/t/variable-in-a-start-process-command/20147>\
**Category:** PowerShell Help\
**Created:** [August 12, 2022, 4:59pm UTC](https://forums.powershell.org/t/variable-in-a-start-process-command/20147 "2022-08-12T16:59:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [August 12, 2022, 4:59pm UTC](https://forums.powershell.org/t/variable-in-a-start-process-command/20147/1 "2022-08-12T16:59:47Z")

</div>

Hello,

I have this code, and I can’t seem to get the logfile from the start-process command to output using hostname-kace\_install.log. It outputs it literally like $Hostname-Kace\_install.log. In the Out-File command it works like I want. I have tried changing the ’ quotes around the entire expression to ", but then I get an error.

`"/i "C:\temp\ampagent.msi" HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log "C:\temp\$Hostname-Kace_install.log" /quiet"`

```auto
Invoke-Command -ComputerName $RemoteComputer -ScriptBlock {
                    $Hostname = hostname
                    Start-Process "msiexec.exe" -ArgumentList '/i "C:\temp\ampagent.msi" HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log "C:\temp\$Hostname-Kace_install.log" /quiet' -Wait -NoNewWindow
                    Get-Service -DisplayName "*Kace*"
                    Get-Service -DisplayName "*Kace*" | Out-File -FilePath "C:\Temp\$Hostname-KaceService.txt"
                    Read-Host -Prompt "Press Enter key to continue"
                        }

```

Thanks for help,

Matt

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 12, 2022, 5:08pm UTC](https://forums.powershell.org/t/variable-in-a-start-process-command/20147/2 "2022-08-12T17:08:37Z")

</div>

You need to put the double quotes outside and the single quotes inside …

```auto
Start-Process "msiexec.exe" -ArgumentList "/i 'C:\temp\ampagent.msi' HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log 'C:\temp\$Hostname-Kace_install.log' /quiet" -Wait -NoNewWindow

```

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [August 12, 2022, 5:14pm UTC](https://forums.powershell.org/t/variable-in-a-start-process-command/20147/3 "2022-08-12T17:14:37Z")

</div>

Variables wont be expanded if they are inside single quotes, even when used in double quotes if the entire expression is wrapped again in single quotes.

```auto
$Hostname = hostname
Write-Output "this expands $Hotname"

Write-Output 'Inside single quotes "$Hotname" it wont expand even with double quotes'

```

so you can build the string to handle this situation either using appending or formating.

```auto
#Appending
... -ArgumentList ('/i "C:\temp\ampagent.msi" HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log "C:\temp\' + $Hostname + '-Kace_install.log" /quiet')

#Formating
$Argument = '/i "C:\temp\ampagent.msi" HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log "C:\temp\{0}-Kace_install.log" /quiet' -f $Hostname

... -ArgumentList $Argument

```

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [August 12, 2022, 5:25pm UTC](https://forums.powershell.org/t/variable-in-a-start-process-command/20147/4 "2022-08-12T17:25:22Z")

</div>

ok Ill try those as well kvprasoon, and Thanks Olaf as well.

I tried Olafs suggestion , tried this:

```auto
$RemoteComputer = Read-Host -Prompt 'Please enter computer name to Install Kace on'
$SessionRemote = New-PSSession $RemoteComputer
Copy-Item -path 'C:\Temp\ITScript\KaceInstall\ampagent.msi' -Destination 'c:\Temp' -ToSession $SessionRemote
                    Invoke-Command -ComputerName $RemoteComputer -ScriptBlock {
                    $Hostname = hostname
                    Start-Process "msiexec.exe" -ArgumentList "/i "C:\temp\ampagent.msi" HOST=xxxx.xxxxxx.xxx NOHOOKS=1 /qn /norestart /log 'C:\temp\$Hostname-Kace_install.log' /quiet" -Wait -NoNewWindow
                    Get-Service -DisplayName "*Kace*"
                    Get-Service -DisplayName "*Kace*" | Out-File -FilePath "C:\Temp\$Hostname-KaceService.txt"
                    Read-Host -Prompt "Press Enter key to continue"
                        }

```

and get this:

```auto
Copy-Item "C:\Temp\*-KaceService.txt" -Destination "C:\Temp\ITScript\KaceInstall\RemoteInstallLogs\" -FromSession $SessionRemote
Write-Host "Logged to C:\Temp\ITScript\KaceInstall\RemoteInstallLogs\$RemoteComputer-KaceService.txt"
Please enter computer name to Install Kace on: testserver
A positional parameter cannot be found that accepts argument 'C:\temp\ampagent.msi
HOST=xxxxx.xxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log 'C:\temp\TESTServer-Kace_install.log' /quiet'.
    + CategoryInfo : InvalidArgument: (:) [Start-Process], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.StartProcessCommand
    + PSComputerName : testserver

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 12, 2022, 5:26pm UTC](https://forums.powershell.org/t/variable-in-a-start-process-command/20147/5 "2022-08-12T17:26:05Z")

</div>

> [@kvprasoon](#):
>
> so you can build the string to handle this situation either using appending or formating.

I’d say there are even more ways to deal with such a situation. 😉

You could make it a real argument _ **list** _

```auto
Start-Process "msiexec.exe" -ArgumentList "/i 'C:\temp\ampagent.msi'", 'HOST=xxx.xxxxxxxxx.xxx', 'NOHOOKS=1', '/qn', '/norestart' "/log 'C:\temp\$Hostname-Kace_install.log'", '/quiet' -Wait -NoNewWindow

```

… or you could use only double quotes and escape the inner ones …

```auto
Start-Process "msiexec.exe" -ArgumentList "/i ""C:\temp\ampagent.msi"" HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log ""C:\temp\$Hostname-Kace_install.log"" /quiet" -Wait -NoNewWindow

```

… or if the path you want to provide does not have any speces you could ommit the inner quotes …

```auto
Start-Process "msiexec.exe" -ArgumentList "/i C:\temp\ampagent.msi HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log C:\temp\$Hostname-Kace_install.log /quiet" -Wait -NoNewWindow

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 12, 2022, 5:30pm UTC](https://forums.powershell.org/t/variable-in-a-start-process-command/20147/6 "2022-08-12T17:30:01Z")

</div>

> [@matt1974](#):
>
> I tried Olafs suggestion , tried this:

The code you posted is not what I suggested. 😉

… and copying files on your local computer does affect the remote computer in any way! 👆🏽 😏

---

<div class="post-metadata">

**Author:** ![matt1974](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@matt1974](https://forums.powershell.org/u/matt1974)\
**Post date:** [August 12, 2022, 5:42pm UTC](https://forums.powershell.org/t/variable-in-a-start-process-command/20147/7 "2022-08-12T17:42:13Z")

</div>

eh? I thought it was!

Anyway, this:  
… or you could use only double quotes and escape the inner ones …

works great, thanks again.

-Matt

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 12, 2022, 6:56pm UTC](https://forums.powershell.org/t/variable-in-a-start-process-command/20147/8 "2022-08-12T18:56:14Z")

</div>

> [@matt1974](#):
>
> eh? I thought it was!

Just to show what the mistake was …

I recommended this:

```auto
Start-Process "msiexec.exe" -ArgumentList "/i 'C:\temp\ampagent.msi' HOST=xxx.xxxxxxxxx.xxx NOHOOKS=1 /qn /norestart /log 'C:\temp\$Hostname-Kace_install.log' /quiet" -Wait -NoNewWindow

```

and what you posted was this:

```auto
Start-Process "msiexec.exe" -ArgumentList "/i "C:\temp\ampagent.msi" HOST=xxxx.xxxxxx.xxx NOHOOKS=1 /qn /norestart /log 'C:\temp\$Hostname-Kace_install.log' /quiet" -Wait -NoNewWindow

```

---

<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:16pm UTC](https://forums.powershell.org/t/variable-in-a-start-process-command/20147/9 "2024-05-16T20:16:46Z")

</div>

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


