PowerShell file operations
last modified February 15, 2025
In this article we show how to work with files in PowerShell.
PowerShell is a cross-platform task automation solution made up of a command-line shell, a scripting language, and a configuration management framework.
PowerShell provides a rich set of cmdlets for file operations.
Get-ChildItem
The Get-ChildItem cmdlet gets the items in one or more specified locations.
Get-ChildItem -Path "C:\"
In this program, we get the child items of the root of the C drive.
PS C:\> .\get_child_item.ps1
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 15/02/2025 16:23 Config.Msi
d----- 15/02/2025 16:23 Documents and Settings
d----- 15/02/2025 16:23 PerfLogs
d----- 15/02/2025 16:23 Program Files
d----- 15/02/2025 16:23 Program Files (x86)
d----- 15/02/2025 16:23 Recovery
d----- 15/02/2025 16:23 System Volume Information
d----- 15/02/2025 16:23 Users
d----- 15/02/2025 16:23 Windows
We run the script and see the output.
Remove-Item
The Remove-Item cmdlet deletes items such as files, directories, and registry keys.
Remove-Item -Path "test.txt"
In this program, we delete a file named test.txt.
PS C:\> .\remove_item.ps1
We run the script and see no output.
Copy-Item
The Copy-Item cmdlet copies an item from one location to another.
Copy-Item -Path "test.txt" -Destination "test_copy.txt"
In this program, we copy a file named test.txt to a new file named test_copy.txt.
PS C:\> .\copy_item.ps1
We run the script and see no output.
Move-Item
The Move-Item cmdlet moves an item from one location to another.
Move-Item -Path "test.txt" -Destination "test_moved.txt"
In this program, we move a file named test.txt to a new file named test_moved.txt.
PS C:\> .\move_item.ps1
We run the script and see no output.
Test-Path
The Test-Path cmdlet tests whether a path exists.
Test-Path -Path "test_moved.txt"
In this program, we test whether a file named test_moved.txt exists.
PS C:\> .\test_path.ps1 True
We run the script and see the output.
Source
In this article we have shown you how to work with files in PowerShell.
Author
List all PowerShell tutorials.