File Names and File Paths

There are a number of ways and a number of cmdlets to work with file names and file paths, which are collected and summarised here.

  • Move-Item - great for move files, as well as other things like Registry entries
  • Remove-Item - will remove files, directories, registry keys etc
  • Test-Path - returns true or false if the path exists, works with file, registry keys and more
  • Split-Path - handy for getting part of a path

Joining Path Elements

The common and most obvious, PowerShell way is to use Join-Path, which actually does a great job and is very good. With PowerShell Core in Linux and macOS it does but the path separator the right way round for the platform, which is good.

When joining multiple path elements you might find this easier:
[System.IO.Path]::Combine(".", "foo", "bar")
This works in PowerShell Core on macOS and hence depending on your platform will give you "./foo/bar" or ".\foo\bar" which is nice. The Join-Path equivalent is
Join-Path -Path (Join-Path -Path "." -ChildPath "foo") -ChildPath "bar"
Unless you have PowerShell 6, in which case you can use:
Join-Path -Path "." -ChildPath "foo" -AdditionalChildPath "bar"
One issue I found with the Combine approach is that it does not handle the following very well:
[System.IO.Path]::Combine(".\", "\foo", "\bar")
This is when Join-Path does actually work better.