Managing Files and Folders

Managing Files and Folders in PowerShell: a hands-on lesson with runnable examples you can edit and execute in the browser.

Cmdlets dealing with files generally use the noun "Item".

# Checking if a file or folder exists
if (Test-Path -Path "C:\Logs") {
    Write-Output "Directory exists."
} else {
    # Create the folder
    New-Item -Path "C:\Logs" -ItemType Directory
}

# Listing files (Dir/ls)
Get-ChildItem -Path "C:\Configs" -Filter "*.json" -Recurse

# Reading and Writing content
$logData = Get-Content -Path "C:\Logs\app.log"
"New log entry" | Out-File -FilePath "C:\Logs\app.log" -Append

# Copying / Moving
Copy-Item -Path "C:\Logs.txt" -Destination "D:\Backup\"