title: How to save Powershell output to specific folder on the local machine when running an Invoke command on remote machine
body: <p>I am using powershell to pull basic computer information from all computers on a LAN. These computers are not, and will never be on, a domain. I have had some success in my test runs getting the output for all of the machines to save into the c:\scripts folder on the host machine. I am, however, having to use the Invoke-command for every cmdlet so that I can put the Output destination outside of the {}.</p>
  <pre><code>$computers = Get-Content -Path 'c:\scripts\livePCs.txt'
  
  foreach ($computer in $computers) {
  $username = '$computer\Administrator'
  $password = Get-Content 'C:\scripts\encrypted_password.txt' | ConvertTo-SecureString
  $credential = New-Object System.Management.Automation.PSCredential($username, $password)
  
  # Local Mounted Shares Enumeration
  Invoke-Command -Computername $computer -Credential $credential {Get-SmbMapping | Format-Table -AutoSize} | Out-File &quot;c:\scripts\ComputerInformation\$computer.mountedshares.ps.txt&quot;
  
  # Local Shares Enumeration
  Invoke-Command -Computername $computer -Credential $credential {Get-SmbShare | Format-Table -AutoSize} | Out-File &quot;c:\scripts\ComputerInformation\$computer.localshares.ps.txt&quot;
  </code></pre>
  <p>I would prefer not to have to do this and it becomes problematic when I have to use If/Else statements, where, because I cannot put the destination outside of braces, I get a file cannot be found error (since it is trying to save on the remote host). I tried using a share instead, in the below but now am getting an access to the file path is denied error.</p>
  <pre><code>Invoke-Command -Computername $computer -Credential $credential {
  $computername = hostname.exe
  If ($PSVersionTable.PSVersion -ge '4.0') {
      If (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
          Get-WindowsOptionalFeature -Online -FeatureName *Hyper-V* | Format-Table -AutoSize | Out-File -Width 1024 &quot;\\$env:computername\scripts\ComputerInformation\$computername.hyperv.admin.ps.txt&quot;
          if (Get-Command Get-VM -ErrorAction SilentlyContinue) {
              Get-VM | Format-Table -AutoSize | Out-File -Width 1024 -Append &quot;c:\scripts\ComputerInformation\$computername.hyperv.admin.ps.txt&quot;
              Get-VM | Get-VMNetworkAdapter | Format-Table -AutoSize | Out-File -Width 1024 -Append &quot;c:\scripts\ComputerInformation\$computername.hyperv.admin.ps.txt&quot;
          } else {
              Write-Host -ForegroundColor Yellow &quot;         Hyper-V feature not installed on this host&quot;
          }
      } else {
          Write-Host -ForegroundColor Red &quot;         You do not have required permissions to complete this task ...&quot;
      }
  } else {
      Write-Host -ForegroundColor Red &quot;         This commands requires at least PowerShell 4.0 ... manual inspection is required&quot;
  }
  </code></pre>
  <p>How do I run this one a remote machine using Invoke-Command but save the output to the local c:\scripts folder?</p>