title: Concatenate filename with current directory path
body: <p>I'm trying to execute a PowerShell script that opens an Excel file and does a SaveAs with it. the problem is that it works when I put the full path for the file, but I want it to be directed to the working folder, that way if I take the script and files to other location I don't have to modify the script for each environment.</p>
  
  <pre><code>$excel = New-Object -ComObject Excel.Application
  $excel.Visible = $true
  $excel.DisplayAlerts = $false
  $wb = $excel.Workbooks.Open("E:\TEMP\TEMPLATE4WEEKS.xlsx")#Opening like this works
  
  $wb = $excel.Workbooks.Open(ThisWorkbook.Path &amp; "\TEMPLATE4WEEKS.xlsx") #this is not working 
  $wb.SaveAs(ThisWorkbook.Path &amp; "\TEMPLATE4WEEKSprotected.xlsx",[Type]::Missing,"password") #this is not working
  $excel.Quit()
  </code></pre>
  
  <p>I was trying to concatenate current folder so it will work if I say move the entire folder to another path.</p>
  
  <p>Right now the folder that contains the PS1 script is the same that contains the TEMPLATE4WEEKS.xlsx file. I want all to operate inside the same folder.</p>
  
  <p>Changed as per Ansgar's response:</p>
  
  <pre><code>$excel = New-Object -ComObject Excel.Application
  $excel.Visible = $true
  $excel.DisplayAlerts = $false
  $file = Join-Path $excel.ThisWorkbook.Path "TEMPLATE4WEEKS.xlsx"
  $wb = $excel.Workbooks.Open($file)
  $wb.SaveAs($file,[Type]::Missing,"password")
  $excel.Quit()
  </code></pre>
  
  <p>I'm getting this error now:</p>
  
  <pre>
  Join-Path : Cannot bind argument to parameter 'Path' because it is null.
  At C:\Users\AGUIRRG2\Desktop\Newfolder\PasswordProtect4WEEKSCopy.ps1:4 char:19
  + $file = Join-Path $excel.ThisWorkbook.Path "TEMPLATE4WEEKS.xlsx"
  +                   ~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidData: (:) [Join-Path],     ParameterBindingValidationException
      + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCommand
  
  Sorry, we couldn't find . Is it possible it was moved, renamed or deleted?
  At C:\Users\AGUIRRG2\Desktop\Newfolder\PasswordProtect4WEEKSCopy.ps1:5 char:1
  + $wb = $excel.Workbooks.Open($file)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : OperationStopped: (:) [], COMException
      + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
  
  You cannot call a method on a null-valued expression.
  At C:\Users\AGUIRRG2\Desktop\Newfolder\PasswordProtect4WEEKSCopy.ps1:6 char:1
  +         $wb.SaveAs($file,[Type]::Missing,"password")
  +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  </pre>