Powershell – Working with files and folders

Cmdlets resume

These are the key file system cmdlets:

Cmdlet Name Alias Cmd Command UNIX sh Command Description
Clear-Item cli N/A N/A Clears content of a file
Copy-Item cpi copy cp Copies file or folder
Get-Content gc type cp Gets the content of a file
Get-Location gl cd pwd Gets the current directory
Move-Item mi move mv Moves file or folder
New-Item ni, md N/A N/A Creates file or folder
Remove-Item ri del, rd rm, rmdir Deletes file or folder
Rename-Item rni, rn ren mv Renames file or folder
Set-Content sc N/A N/A Sets file content
Set-Item si N/A N/A Sets file content
Set-Location sl cd, chdir cd, chdir Sets current directory

Copy files or folders
To copy a file or a folder to a new location, type:

Copy-Item c:\scripts\test.txt c:\test

To copy every text files, type:

Copy-Item c:\scripts\*.txt c:\test

To copy a folder to a new location with all its content, type:

Copy-Item c:\scripts c:\test -recurse

Note: Copy-Item alias is cpi, cp or copy
Create new file or folder
To create a new directory (named Poweshell), type:

New-Item c:\scripts\Windows PowerShell –type directory

To create a new text file, type:

New-Item c:\scripts\Windows\PowerShell\new_file.txt –type file

To create a new text file replacing an existing one with the same name and adding some text, type:

New-Item c:\scripts\Windows\PowerShell\new_file.txt -type file -force -value "This is text added to the file"

Note: New-Item alias is ni
Delete file or folder
To delete a file, type:

Remove-Item c:\scripts\Windows\PowerShell\new_file.txt

To remove all files of a folder, type:

Remove-Item c:\scripts\Windows\PowerShell\*

To remove all folder files except those with wav extension and bypassing the confirmation prompt (with the recurse parameter), type

Remove-Item c:\scripts\Windows\PowerShell\* -recurse –exclude *.wav

To remove all files with wav and mp3 extension from a folder and bypassing the confirmation prompt (with the recurse parameter), type

Remove-Item c:\scripts\Windows\PowerShell\* -recurse –include .wav,.mp3

To delete all text files except those who have the string vamue test in file name, type:

Remove-Item c:\scripts\Windows\PowerShell\* –include *.txt –exclude *test*

To simulate the execution of the delete cmdlet without really implementing it use the whatif parameter and for every file\folder you will receive a notification :

Remove-Item c:\scripts\Windows\PowerShell\* –include *.txt –exclude *test* -whatif

In order to remove an alias (named show), type:

Remove-Item alias:\show

Note: To switch to a powershell drive (in this case the environment variables) use this command:

Set-Location env:\

Move file or folder
To move a file, type:

Move-Item c:\scripts\Windows\PowerShell\new_file.txt c:\test

To move all text files and force overwrite if a file exists with the same name in destiny, type:

Move-Item c:\scripts\Windows\PowerShell\*.txt c:\test -force

For a single file you’re able to move and rename it with these parameters:

Move-Item c:\scripts\Windows\PowerShell\new_file.txt c:\test\renamed_file.txt

Note: Move-Item alias is mi, mv, move
Rename file or folder
To rename a file, type:

Rename-Item c:\scripts\Windows\PowerShell\new_file.txt new_name.txt

Note: Rename-Item alias is rni or ren
Replicate dir command
To get the similar of a dir command, type:


To list all files and folder in c: subfolders and all files (including hidden and system items), type:

Get-ChildItem –force c:\ -recurse

You can with the same cmdlet work with environment variables like:

Get-ChildItem env:
Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

You can also work with exclude and include parameters

Get-ChildItem c:\scripts\*.* -include *.txt,*.log
Get-ChildItem c:\scripts\*.* -exclude *.txt,*.log

Using the Sort_Object parameter:

Get-ChildItem c:\scripts\*.* | Sort-Object length –descending

Note: Get-ChildItem alias is gci, ls or dir
Retrieve specific item
To retrieve a specific item such as file, folder, registry ket… use Get-Item cmdlet
To obtain properties of an item (first example a file, second a registry key), type:

$(Get-Item c:\scripts).lastaccesstime
$(Get-Item hkcu:\software).subkeycount

Note: To see properties of an item use Get-Member:

Get-Item hkcu:\software | Get-Member

Note: Get-Item alias is gu
Verify existence of file or folder
You can check if a file or files (or folders) exists. It will return a Boolean with the result.

Test-Path c:\scripts\test.txt

You can do it with wildcards to check if any file exists:

Test-Path c:\scripts\*.wav

You can do work with different items like registry keys:

Test-Path HKCU:\Software\Microsoft\Windows\CurrentVersion


In Depth

Tested With
PowerShell 4.0

Categories: Powershell | Leave a comment

Post navigation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: