PowerShell FileSystem Drives
The FileSystem provider exposes files and folders as a drive you can navigate. You move into folders and list their files with ordinary navigation and listing commands.
List only the files you created
Create a folder and two files, move into it, then list the files by name.
$null = New-Item -ItemType Directory -Path "notes"
Set-Content -Path "notes/a.txt" -Value "apple"
Set-Content -Path "notes/b.txt" -Value "berry"
Set-Location "notes"
(Get-ChildItem -File -Filter *.txt | Sort-Object Name | Select-Object -ExpandProperty Name) -join ", "Print just the folder name
Split-Path -Leaf turns a location into its last part only.
$null = New-Item -ItemType Directory -Path "notes"
Set-Location "notes"
Split-Path -Leaf (Get-Location).PathMove down and back up
Go into a subfolder, print its name, then step back and print the parent name.
$null = New-Item -ItemType Directory -Path "notes"
$null = New-Item -ItemType Directory -Path "notes/inner"
Set-Location "notes/inner"
Split-Path -Leaf (Get-Location).Path
Set-Location ..
Split-Path -Leaf (Get-Location).PathExample
$null = New-Item -ItemType Directory -Path "notes"
Set-Content -Path "notes/a.txt" -Value "apple"
Set-Content -Path "notes/b.txt" -Value "berry"
$null = New-Item -ItemType Directory -Path "notes/inner"
Set-Location "notes"
$files = (Get-ChildItem -File -Filter *.txt | Sort-Object Name | Select-Object -ExpandProperty Name) -join ", "
"Files: $files"
Set-Location "inner"
"Now in: " + (Split-Path -Leaf (Get-Location).Path)
Set-Location ..
"Back in: " + (Split-Path -Leaf (Get-Location).Path)Key points
- FileSystem provider: the provider that exposes files and folders as drives.
- Use Set-Location to move into a folder.
- Use Get-ChildItem -File to list only files, never folders.
- Use Sort-Object Name so a listing always comes out in the same order.
- Use Split-Path -Leaf to print just a folder name, never a full path.
- Use Set-Location .. to move back up one folder.
In this example, a folder with two files and a subfolder is created first. The script moves into the folder and lists its files, steps into the subfolder and prints its name, then steps back and prints the parent name.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which command moves you into another folder?