title: PowerShell passing parameter with Invoke-Command
body: <p>I can't seem to find the correct syntax to pass 2 variables from the CALL-script to the execution script in order to have it executed on the remote server. I tried single quotes, double quotes, brackets, .. nothing I can fiind passes the <code>$Target</code> and <code>$OlderThanDays</code> parameters to the script. </p>
  
  <p>Thank you for your help.</p>
  
  <p><strong>The CALL-Script:</strong></p>
  
  <pre><code>#================= VARIABLES ==================================================
  $ScriptDir = "\\Server\Scripts"
  #================= BODY =======================================================
  # Invoke-Command -ComputerName SERVER1 -FilePath $ScriptDir\"Auto_Clean.ps1"         
  
  Invoke-Command -FilePath .\Test.ps1 -ComputerName SERVER01 -ArgumentList {-Target ´E:\Share\Dir1\Dir2´,-OlderThanDays ´10´}
  </code></pre>
  
  <p><strong>The execution Script:</strong></p>
  
  <pre><code>#================= PARAMETERS =================================================
  Param(
    [Parameter(Mandatory=$True,Position=1)]
     [string]$Target,
  
     [Parameter(Mandatory=$True,Position=2)]
     [string]$OlderThanDays
  )
  
  #================= BODY =======================================================
  # Set start time &amp; logname
  $StartTime = (Get-Date).ToShortDateString()+", "+(Get-Date).ToLongTimeString()
  $LogName = "Auto_Clean.log"
  
  # Format header for log
  $TimeStamp = (Get-Date).ToShortDateString()+" | "+(Get-Date).ToLongTimeString()+" |"
  $Header = "`n$TimeStamp Deleting files and folders that are older than $OlderThanDays days:`n"
  Write-Output  "$Header" # to console
  Out-File $Target\$LogName -inputobject $Header -Append # to log
  # PS 2.0 Workaround (`tee-object -append`) // PS 4.0: `Write-Output "`nDeleting folders that are older than $OlderThanDays days:`n" | Tee-Object $LogFile -Append` 
  
  # Remove files older than
  Get-ChildItem -Path $Target -Exclude $LogName -Recurse | 
      Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$OlderThanDays) } | ForEach {
          $Item = $_.FullName
          Remove-Item $Item -Recurse -Force -ErrorAction SilentlyContinue
          $Timestamp = (Get-Date).ToShortDateString()+" | "+(Get-Date).ToLongTimeString()      
          # If folder can't be removed
          if (Test-Path $Item)
              { "$Timestamp | FAILLED: $Item (IN USE)" } 
          else
              { "$Timestamp | REMOVED: $Item" }  
          } | Out-File $Target\$LogName -Append
          # PS 4.0: ´| Tee-Object $Target\$LogName -Append` # Output folder names to console &amp; logfile at the same time
  
  # Remove empty folders   
  while (Get-ChildItem $Target -recurse | where {!@(Get-ChildItem -force $_.FullName)} | Test-Path) {
      Get-ChildItem $Target -recurse | where {!@(Get-ChildItem -force $_.FullName)} | Remove-Item
  }             
  
  # Format footer
  $EndTime = (Get-Date).ToShortDateString()+", "+(Get-Date).ToLongTimeString()
  $TimeTaken = New-TimeSpan -Start $StartTime -End $EndTime
  
  Write-Output ($Footer = @"
  
     Start Time          : $StartTime
     End Time            : $EndTime
     Total Runtime       : $TimeTaken
  $("-"*79)
  "@)
  
  # Write footer to log
  Out-File -FilePath $Target\$LogName -Append -InputObject $Footer
  
  # Clean up variables
  $Target=$StartTime=$EndTime=$OlderThanDays = $null
  </code></pre>
  
  <p>The execution script:</p>