Example input

$Path = ".\README.md"
$Path = $env:UserProfile

Require all paths to exist, by stopping at every error

$ErrorActionPreference = [Management.Automation.ActionPreference]::Stop

String

Get path of existing file or folder

[String] $fileOrFolder = $Path -replace '[\\/]$', '' | Convert-Path

Fake path of file or folder

[String] $file = $Path | foreach {
    $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($_)
}
[String] $folder = $Path -replace '[\\/]$', '' | foreach {
    $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($_)
}

Filesystem Information Object

Files and folders contain FullName, Extension, Name, Exists, CreationTime, CreationTimeUtc, LastAccessTime, LastAccessTimeUtc, LastWriteTime, LastWriteTimeUtc, Attributes.

Files also contain Length, DirectoryName, Directory, IsReadOnly.

Folders also contain Parent, Root.

Retrieve information of existing file or folder

[IO.FileInfo] $visibleFile = $Path | Get-Item
[IO.DirectoryInfo] $visibleFolder = $Path -replace '[\\/]$', '' | Get-Item
[IO.FileSystemInfo] $visibleFileOrFolder = $Path -replace '[\\/]$', '' | Get-Item
[IO.FileInfo] $anyFile = $Path | Get-Item -Force
[IO.DirectoryInfo] $anyFolder = $Path -replace '[\\/]$', '' | Get-Item -Force
[IO.FileSystemInfo] $anyFileOrFolder = $Path -replace '[\\/]$', '' | Get-Item -Force

Retrieve information of existing file or folder
or fake information of non-existing file or folder

[IO.FileInfo] $file = $Path | foreach {
    $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($_)
} | foreach { 
    [IO.FileInfo]::new($_)
}
[IO.DirectoryInfo] $folder = $Path -replace '[\\/]$', '' | foreach {
    $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($_)
} | foreach { 
    [IO.DirectoryInfo]::new($_)
}

Sources:

Related:

Tags:
File System - Use paths, get meta data, link, download, and encrypt files and folders