PowerShell Files and Folders Tutorials

If you want to learn PowerShell, then you should check out various operations you can do with files and folders in PowerShell.

This page contains all the PowerShell files and folders tutorials.

Working with Files in PowerShell

Let us explore how to work with files in PowerShell, including creating, reading, writing, copying, and deleting files.

Creating Files using PowerShell

To create a new file using PowerShell, you can use the New-Item cmdlet. The -Path parameter specifies the location of the new file, while the -ItemType parameter is set to File. Here’s an example of how to create a new text file named example.txt in the current directory:

New-Item -Path .\example.txt -ItemType File

Writing to Files in PowerShell

Once you have created a file in PowerShell, you may want to write content to it. PowerShell provides several cmdlets for writing data to files, such as Set-ContentAdd-Content, and Out-File. The Set-Content cmdlet is used to write or replace content in a file:

Set-Content -Path .\example.txt -Value "This is a line of text."

If you want to append content to a file without overwriting the existing content, you can use the Add-Content cmdlet:

Add-Content -Path .\example.txt -Value "This is another line of text."

Reading from Files in PowerShell

To read the content of a file in PowerShell, you can use the Get-Content cmdlet. This cmdlet reads each line of a file and returns an array of strings:

Get-Content -Path .\example.txt

Copying Files using PowerShell

Copying files is a common task, and PowerShell makes it easy with the Copy-Item cmdlet. You can specify the source file and the destination where the file should be copied:

Copy-Item -Path .\example.txt -Destination .\backup\example_backup.txt

Deleting Files in PowerShell

If you need to delete a file in PowerShell, you can use the Remove-Item cmdlet. Be cautious with this cmdlet, as it will permanently delete the specified file:

Remove-Item -Path .\example.txt

Listing Files and Folders in PowerShell

To list all files and folders within a directory in PowerShell, you can use the Get-ChildItem cmdlet. This cmdlet can be used to display a list of items in the current directory or any specified path:

Get-ChildItem -Path .\

PowerShell Files Tutorials

Here is the list of PowerShell file tutorials:

PowerShell file tutorialWhat you will learn
PowerShell Test-PathLearn to check if files and folders exist and handle missing paths safely.
How to Use PowerShell Import-Csv Cmdlet?Import CSV data into objects so you can filter, sort, and report easily.
How to Use PowerShell Export-CSV cmdlet?Export command output to CSV files for sharing and further analysis.
Check if a File Exists in PowerShellUse different techniques to verify file existence before processing it.
How To Create File If Not Exists In PowerShell?Create files only when they are missing to avoid overwriting data.
How To Delete File If Exists In PowerShell?Delete files conditionally and prevent errors from non‑existent paths.
Download File from URL in PowerShellDownload files from the internet using cmdlets like Invoke-WebRequest.
Save Output to File in PowerShellRedirect command output into text files for logging or reuse later.
Replace Text in a File Using PowerShellSearch and replace strings inside files with simple scripts.
How to Read a File Line by Line in PowerShell?Process large text files line by line without loading everything into memory.
How to Find Strings in Files in PowerShell?Search for specific text or patterns across one or more files.
How to Write String to File in PowerShellWrite single or multiple strings to text files using core cmdlets.
How to Copy Files from One Folder to Another in PowerShell?Copy files between folders with filters and overwrite options.
How to Write Variables to a File in PowerShell?Persist variable values and objects into files for later use.
Move a File to a Folder in PowerShellMove files to other directories while optionally changing the name.
Get File Name from Path in PowerShellExtract just the file name or extension from a full path string.
Count Lines in a File in PowerShellCount how many lines a text file contains for reporting or checks.
PowerShell Foreach File in FolderLoop through each file in a directory and run actions on them.
Copy and Rename Files in PowerShellCopy files and assign new names in bulk with scripts.
How to Delete a File in PowerShell?Remove files with Remove-Item and add safety checks as needed.
How to Write JSON to File in PowerShell?Convert data to JSON and save it for configuration or APIs.
How to Read CSV File Line by Line in PowerShell?Iterate through CSV rows one at a time for fine‑grained processing.
How to Check if a File Contains a String in PowerShell?Test whether a file contains specific text and return matches.
How to Get the Size of a File in PowerShell?Retrieve file sizes and display them in KB, MB, or other units.
PowerShell Write-Host to FileCapture Write-Host output into log or transcript files.
How to List File Names Only in PowerShell?List only file names without paths or extra metadata.
PowerShell Write to Log FileBuild reusable logging patterns that append to log files.
How to Log Error Messages to a File Using PowerShell Try Catch?Trap exceptions and write detailed error information to files.
How to Create a Log File with Date and Time in PowerShell?Create timestamped log files for each run or day automatically.
How to Read Log Files with PowerShell?Parse and filter log files to extract the information you need.
How to Read the First Line of a File in PowerShell?Quickly read only the first line of large text files.
How to Write Multiple Lines to a File in PowerShell?Write arrays or multiple strings to a file in a single operation.
How to Encrypt a File with a Password in PowerShell?Protect file contents with encryption and password‑based access.
How to Rename Files with Date in PowerShell?Append dates to file names in bulk for versioning or archiving.
How to Find File by Name in PowerShell?Find files by exact name or simple patterns in folders.
How to Get Details of a File in PowerShell?Display file metadata such as attributes, timestamps, and size.
How to Get File Creation Date in PowerShell?Read file creation time for audit and reporting purposes.
How to Get File Version in PowerShell?Retrieve version details from executables and DLL files.
How to Get File Extensions from Filenames in PowerShell?Work with file extensions for filtering and branching logic.
How to Check if a File is Empty with PowerShell?Detect empty files before processing or deleting them.
How to Split Large Text Files with PowerShell?Split big text files into smaller chunks by size or line count.
How To Get Newest File In Directory In PowerShell?Identify the most recently created or modified file in a folder.
How to Create an Empty File in PowerShell?Create blank files quickly for testing or placeholders.
How to Get File Name Without Extension in PowerShell?Strip extensions from file names for display or renaming.
How to Delete Files Older Than X Days in PowerShell?Automate cleanup of files based on their age in days.
How to Check if a File Was Modified in the Last 24 Hours in PowerShell?Compare modification times against the last 24 hours.
How to Convert Files to Base64 in PowerShell?Encode file contents as Base64 for APIs or secure transport.
How to Get MD5 Hash of a File in PowerShell?Generate MD5 checksums to verify file integrity.
How to Unblock Files Recursively using unblock-file in PowerShell?Unblock downloaded files across folders in one go.
How to search for files recursively in PowerShell?Search entire directory trees for matching file names.
PowerShell Find and Replace in FileAutomate find‑and‑replace operations across many files.
How to Append Text to a File in PowerShell?Add new content to the end of existing files safely.
How to Count Words in a File Using PowerShell?Count words in text files for stats or validations.
How to Count Duplicate Lines in a Text File Using PowerShell?Detect and count repeated lines in logs or data files.
How to Get Unique Lines from a File Using PowerShell?Filter out duplicates and keep only unique lines.
How to Get Unique Values from CSV Using PowerShell?Extract distinct values from CSV columns for analysis.
How to Get the First 10 Files in a Folder Using PowerShell?Select only the first N files from a directory listing.
How to Convert JSON to CSV in PowerShell?Convert JSON data into CSV for Excel and reporting.
How to Sort Files by Date in PowerShell?Sort files by created or modified dates for processing.
How to Add Date to Filename Using PowerShell?Automatically add current date strings to file names.
How to Convert CSV to HTML Table in PowerShell?Render CSV data as HTML tables for web or email use.
How to Get the First and Last Line of a CSV File in PowerShell?Quickly inspect the header and last row of CSV files.
PowerShell Copy-ItemUse Copy-Item for flexible file and folder copy tasks.
PowerShell Copy-item Create Folder If Not ExistEnsure destination folders exist before copying items.
How to Use PowerShell to Find Files Modified After a Certain Date?Filter files by modification date for audits or cleanup.
How to Find and Remove Empty Folders Using PowerShell?Locate and delete empty directories left by applications.
How to Find All Files with a Specific Extension Using PowerShell?List all files with selected extensions like .txt or .log.
How to Create CSV Files with Headers in PowerShell?Create CSV files with named columns programmatically.
How to Open a File in PowerShell?Launch files with their default applications from scripts.
How to Remove the First and Last Lines from a File Using PowerShell?Trim header and footer lines from structured text files.
How to Remove Blank Lines from PowerShell Format-Table Output?Clean up formatted table output before saving to files.
How to Create JSON Files with Content Using PowerShell?Build JSON objects in scripts and write them to disk.
How to Remove Blank Lines from CSV Files Using PowerShell?Sanitize CSV files by removing empty records.
Create XML Files with Content Using PowerShellGenerate XML documents and populate nodes and values.
How to Create a File with Date in the Name Using PowerShell?Create new files whose names include the current date.
How to Remove the Last Empty Line from a File Using PowerShell?Remove trailing blank lines from text files.
How to Remove the Last Line from a File Using PowerShell?Drop the final line of a file for cleanup or reformatting.
How to Read a Text File in PowerShell and Skip the First Line?Skip headers while processing the rest of a text file.
How to Create a File in the Current Directory Using PowerShell?Create new files quickly in the current working folder.
How to Get All Files in a Directory Using PowerShell?Retrieve complete file listings from a directory.
How to Create Files with Content Using PowerShell?Create new files and populate them with content in one step.
How to List Directories and Files in PowerShellShow both folders and files in structured directory listings.
How to Find Files Modified Between Dates Using PowerShell?Find files whose modification times fall between two dates.
Create a Folder with Today’s Date and Copy Files to it using PowerShellAutomate dated archive folders and copy files into them.
How to Show Progress When Copying Files with PowerShell Copy-ItemDisplay progress while long‑running copy operations execute.
How to Find Files Modified in the Last 24 Hours in PowerShell?List files that have changed within the last 24 hours.
How to Check Who Modified a File Last in Windows Using PowerShellUse logs and metadata to see who last changed a file.
PowerShell Script to Delete Files Older Than 30 DaysSchedule scripts to remove files older than 30 days.
Get Files Older Than 1 Month with PowerShellList files older than one month for review or deletion.
How to Find Files Modified After a Specific Date Using PowerShell?Locate files modified after a fixed cutoff date.
How to List Hidden Files in PowerShell?Reveal and list hidden files that are normally hidden.
PowerShell Remove-Item Cmdlet [With Examples]Delete files and folders safely with Remove-Item options.
PowerShell Out-File [With Examples]Send output to files with control over encoding and width.
PowerShell Convert XML to CSVConvert XML data into CSV format for simpler processing.
How to Test If a File Exists in PowerShell?Combine Test-Path and other checks to validate file presence.
How to Convert JSON to XML using PowerShell?Convert JSON documents into XML format using cmdlets.
How to Convert HEIC to JPG using PowerShellBatch‑convert HEIC images to widely supported JPG files.
PowerShell Get-ChildItem Filter ExamplesFilter directory listings by name, extension, and more.
PowerShell Get-ChildItem Files OnlyList only files (excluding folders) using Get-ChildItem.
How to Find Passwords in Files with PowerShell?Scan files for patterns that may contain passwords.
How to Find the Most Recent File in a Directory with PowerShell?Identify the latest modified file in any directory.
PowerShell Find File by Name Wildcard ExamplesSearch for files using wildcard patterns like * and ?.
How to Convert TXT to CSV with PowerShell?Parse delimited text and output structured CSV files.
Convert Excel Files to CSV Using PowerShellAutomate converting Excel workbooks into CSV files.
Check if a File’s LastWriteTime is Greater Than a Specific Date in PowerShellCheck if a File’s LastWriteTime is Greater Than a Specific Date in PowerShell
How to Move Files from One Folder to Another Using PowerShell (With Date Filtering)Learn how to move files from one folder to another folder using PowerShell with date filtering.

Working with Folders in PowerShell

PowerShell is a powerful tool for managing files and handling directories or folders on your system. Whether you need to create, navigate, list, or remove directories, PowerShell offers a suite of cmdlets. Let us explore how to work with folders in PowerShell.

Creating Folders in PowerShell

To create a new folder in PowerShell, you can use the New-Item cmdlet. The -ItemType parameter should be set to Directory to indicate you’re creating a folder. Here’s an example of creating a new folder named “NewFolder”:

New-Item -Path .\NewFolder -ItemType Directory

If you need to create multiple folders at once, PowerShell can handle that as well. For instance, you can create nested directories in a single command:

New-Item -Path .\ParentFolder\ChildFolder -ItemType Directory

Navigating to Folders in PowerShell

To change your current working directory in PowerShell, you use the Set-Location cmdlet, which is similar to the cd command in traditional command-line interfaces. Here’s how to navigate to a folder named “Documents”:

Set-Location -Path .\Documents

You can also use the shorthand alias cd to change directories in PowerShell:

cd .\Documents

Deleting Folders in PowerShell

To delete a folder in PowerShell, you can use the Remove-Item cmdlet. This operation will delete the folder and all its contents, so it should be used with caution:

Remove-Item -Path .\OldFolder -Recurse

The -Recurse parameter is necessary if the folder contains other items, as PowerShell will not remove a non-empty directory without it.

PowerShell Folders Tutorials

Here is the list of PowerShell folder tutorials:

PowerShell folder tutorialWhat you will learn
Get folder size in GB in PowerShellCalculate the total size of a folder in GB, including all subfolders and files.
How to Count Files in a Folder Using PowerShell?Count how many files exist in a folder, with optional filtering.
How to Delete Contents of Folder in PowerShell?Clear all files and subfolders inside a folder without deleting the folder itself.
How to Create folder if not exist in PowerShell?Create a folder only when it does not already exist to avoid errors.
How to Check if a Folder is Empty in PowerShell?Detect whether a folder contains any files or subfolders.
How to Check If a Folder Exists in PowerShell?Verify that a folder path exists before using it in scripts.
How to Change Directory in PowerShell?Use Set-Location and cd to move between folders in a session.
How to Get Current Directory in PowerShell?Retrieve the current working directory path for use in scripts.
How to Find The Largest Folders Using PowerShell?Identify which folders consume the most disk space on a drive.
How to Rename a Folder in PowerShell if It Exists?Safely rename a folder only when the original folder is present.
How to List Directories in PowerShell?List only folders (excluding files) under a given path.
How to Create a Folder with the Current Date using PowerShell?Create a new folder whose name includes today’s date automatically.
How to Create Folders with Year, Month, and Day Using PowerShell?Generate nested year, month, and day folders for structured storage.
Create Multiple Folders in PowerShellCreate many folders at once from lists, loops, or patterns.
Create Folder Structure from CSV using PowerShellRead folder definitions from a CSV file and build the structure automatically.
How to Set Folder Permissions Using PowerShellAssign or modify NTFS permissions on folders for users and groups.
How to Find Public Folder Paths in PowerShell?Locate and list public folder paths in supported environments.
100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.