title: Exclude SubFolder in Get-ChildItem PowerShell Statement
body: <p>I've been doing my best to get more familiar with PowerShell as a way to automate many of our regular tasks in an effort to eliminate human error.  I support a web based application, and my current goal is to create a script to migrate the application from one server to another.  So far I've gotten the script to install the application on the new server, copy the configuration files from the old server, and backup all the files prior to making any additional changes.  The last thing I need to do is search through the configuration directory and all sub folders to replace all instances of the hostname, the IP address, and the directory location (in the event the drive letter changes on the new server) There is a folder called X:\Website\Tools\Database\Upgrade that contains a folder for every version that the customer has ever been on, and within that folder is the database upgrade script.  I do not have permission to change anything in this folder, and frankly don't really care to change anything within X:\Website\tools.  </p>
  
  <p>I have written this portion of the script as follows:</p>
  
  <pre><code>#Replace all instances of hostname, IP address, and drive letter
  $path = "\\$newip\$newdriveletter$\Website"
  $exclude = @("$path\tools\*")
  $files = get-childitem $path\*.* -recurse -exclude $exclude 
  $files | %{
      (gc $_) -replace $oldhost, $newhost -replace $oldip, $newip -replace 
  "${olddriveletter}:\Website", "${newDriveLetter}:\Website" | set-content 
  $_.fullname
  }
  </code></pre>
  
  <p>When I do this, it replaces everything I'm wanting it to replace, but it's still throwing the following error:</p>
  
  <pre><code>gc : Access to the path 
  '\\10.5.17.60\E$\Website\Tools\Database\Upgrade\6.2.0' is denied.
  At C:\Folder\Powershell\Migrate.ps1:72 char:6
  +     (gc $_) -replace $oldhost, $newhost -replace $oldip, $newip -repl ...
  +      ~~~~~
       + CategoryInfo          : PermissionDenied: 
  (\\10.5.17.60\E$...e\Upgrade\6.2.0:String) [Get-Content], 
  UnauthorizedAccessException
      + FullyQualifiedErrorId : 
  </code></pre>
  
  <p>I've also read that the -exclude parameter is pretty much broken in PowerShell and doesn't work well for excluding folders.  The suggestion was to format it like so:</p>
  
  <pre><code>#Replace all instances of hostname, IP address, and drive letter
  $path = "\\$newip\$newdriveletter$\Website"
  $files = get-childitem $path\*.* -recurse | select-object -expandproperty 
  fullname | where-object{$_ -notlike "\\tools\\"}
  $files | %{
  (gc $_) -replace $oldhost, $newhost -replace $oldip, $newip -replace "${olddriveletter}:\Website", "${newDriveLetter}:\Website" | set-content $_.fullname
  }
  </code></pre>
  
  <p>Unfortunately, when I run this, I get the error:</p>
  
  <pre><code>Set-Content : Cannot bind argument to parameter 'Path' because it is null.
  At C:\Folder\Powershell\Migrate.ps1:72 char:151
  + ... \WebAccess", "${newDriveLetter}:\Website" | set-content $_.fullname
  </code></pre>
  
  <p>So basically I've achieved the end goal, but I'd really like to do this without any errors popping up.  If people start running this and see red they're likely to freak out.  So I'm thinking there has to be a way to exclude even looking into these folders.  Possibly an If statement, but I'm not quite sure how to format that.  Any suggestions?</p>