title: Powershell -renaming a file after copying
body: <p>I'm having ongoing trouble with a script I've written that is (supposed) to do the following.
  I have one folder with a number of csv files, and I want to copy the latest file with the company name into another folder, and rename it.</p>
  
  <p>It is in the current format:</p>
  
  <pre><code>21Feb17070051_CompanyName_Sent21022017
  </code></pre>
  
  <p>I want it in the following format:</p>
  
  <pre><code>CompanyName21022017
  </code></pre>
  
  <p>So I have the following powershell script to do this:</p>
  
  <pre><code>## Declare variables ##
  
  $DateStamp = get-date -uformat "%Y%m%d"
  $csv_dest = "C:\Dest"
  $csv_path = "C:\Location"
  
  ## Copy latest Company CSV file ##
  
  get-childitem -path $csv_path -Filter "*Company*.csv" | 
      where-object { -not $_.PSIsContainer } | 
      sort-object -Property $_.CreationTime | 
      select-object -last 1 |
      copy-item -Destination $csv_dest
  
  ## Rename the file that has been moved ##
  
  get-childitem -path $csv_dest -Filter "*Company*.csv" | 
      where-object { -not $_.PSIsContainer } | 
      sort-object -Property $_.CreationTime | 
      select-object -last 1 | rename-item $file -NewName {"Company" + $DateStamp + ".csv"} 
  </code></pre>
  
  <p>The file seems to copy ok, but the rename fails - </p>
  
  <pre><code>Rename-Item : Cannot bind argument to parameter 'Path' because it is null.
  At C:\Powershell Scripts\MoveCompanyFiles.ps1:20 char:41
  +     select-object -last 1 | rename-item $file -NewName {"CompanyName" + $DateSt ...
  </code></pre>
  
  <p>I think it is something to do with the order in which powershell works, or the fact it can't see the .csv in the $file variable. There are other files (text files, batch files) in the destination, in case that affects things.
  Any help in where I'm going wrong would be appreciated.</p>