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
Copy-Item
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
New-Item
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
Remove-Item
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
Move-Item
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
Rename-Item
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
Get-ChildItem
To get the similar of a dir command, type:

Get-ChildItem

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
Get-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
Test-Path
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
http://blogs.technet.com/b/heyscriptingguy/archive/2009/04/23/how-can-i-work-with-directories-files-and-folders-by-using-windows-powershell.aspx
http://www.techotopia.com/index.php/Working_with_File_Systems_in_Windows_PowerShell_1.0

Tested With
PowerShell 4.0

Advertisements
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

Blog at WordPress.com.

%d bloggers like this: