Working with the file system in PowerShell is not that different from the good old Windows command prompt. Because of aliases and functions you have all the familiar commands at your fingertips: cd, del, copy, mkdir, rmdir, move, … To know the actual PowerShell cmdlet behind one of these aliases, for example copy, use alias copy. You’ll find that the Copy-Item cmdlet is the PowerShell command behind the copy alias.
When you use the PowerShell Community Extensions, the cd alias is replaced by a function with more features. Just type cd ? to find out what you can do.
When you are working with the file system, you are actually working with PS drives. To see the PS drives, type get-psdrive. You will notice PS drives are not reserved for file systems only. PS drives are also used to work with the registry, aliases, environment variables, functions, variables and the certificates store.
To navigate to a PS drive, use CD or set-location followed by the name of the PS drive and a : (e.g. cd HKCU:). A PS drive is provided by, well, a provider. For now, I will limit myself to working with the FileSystem. Look for other posts about working with the registry or certificate stores.
You can create your own PS drives with the new-psdrive cmdlet. This can be useful to create shortcuts to other paths. For example, to create a PS drive to C:\Program Files type the following command:
New-PSDrive pf -PSProvider FileSystem -root “c:\program files”
You can now use CD PF: to navigate to your C:\Program Files folder. Of course, you should add the above command to your profile if you want to have the PS drive available in every shell.
To remove a PS drive, use the REMOVE-PSDRIVE cmdlet. For example:
Remove-PSDrive pf:
Now you know about PS drives, let’s see what more we can do. A lot of times, you will just look for files and work with lists of files and that is where the dir command comes in. dir is an alias for get-childitem.
When you use the PowerShell Community Extensions, dir is not an alias but a function. Just type cat function:/dir to see the definition. The examples below might not work so if they don’t, use the GCI alias instead of DIR.
Some things you can do with dir:
- You can get a listing for multiple locations with one command. For example: dir c:\,C:\users.
- You can pass parameters to dir. To find out which ones, use man get-childitem or man gci. A useful parameter is -recurse to list subdirectories as well.
- Use the -force parameter to see hidden files.
- Use the -name parameter to only see the names of the files and folders.
- Use -include and -exclude with wildcards to specify what you want and don’t want to see. For example: gci -recurse -include *.txt -exclude [ABC]* -name. This shows all txt files but not the ones that start with a, b or c.
What about reading and writing files with PowerShell. Reading is simple with the type alias. The actual cmdlet is called get-content. For example, to show the contents of the file C:\config.sys, use the following command:
TYPE c:\config.sys
Nothing special here but you can add some parameters. For example, the -totalcount parameter allows you to only read a specific number of lines. You can also type multiple files at once by separating these files with a comma. Use help type -detailed for more information.
You can also use ${path} to get the content. For instance ${c:\config.sys} outputs that file’s content.
By the way, if you want to create files from the output of commands use redirection like in cmd.exe. Take the following command:
gci . -recurse -include *.dll > dlls.txt
This command just gets a lists of dll files on the system and outputs the result to the file dlls.txt. You can also use the out-file cmdlet. For example:
gci . -recurse -include *.dll | out-file -filepath $env:personal\dlls.txt
The out-file cmdlet has some interesting parameters like -noclobber. Use get-help out-file -detailed for more information.
New files and folders are created with the new-item cmdlet. To create a new file in your Documents folder (Vista), use the following command:
new-item $env:personal\myfile.txt -ItemType file -force
The parameter -force is used to overwrite an existing file if needed. You can now open the file for editing with notepad $env:personal\myfile.txt. If you have the PowerShell Community Extensions installed, you can use e instead of notepad. You can define the editor you want to use by editing your profile (with the ep command).
You can also open the file with invoke-item. With that cmdlet, the default program for the file extension is used. So if you do invoke-item mydoc.doC Word will be used to open the file. You can also use INVOKE-ITEM . to open the current path in Windows Explorer.
To quickly add some content to a file (a bit like COPY CON in cmd.exe) you can type the following:
new-item $env:personal\myfile.txt -ItemType file -force -value @”
When you press ENTER after the ” you will get a >> prompt where you can type your text. To end your text type “@ on a new line and then press <ENTER> twice. What you have used now is a here string and is useful if you have to type longer text in PowerShell. It also demonstrates that PowerShell can give you multiple lines to finish a command when needed.
For more detailed and more advanced information about working with files and text, take a look at this information.

One Comment
> So if you do invoke-item mydoc.doC Word will be used to open the file.
One of my most frequently used aliases is II (for Invoke-item). This means you can just type:
ii mydoc.doc
or
ii .
Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
Post a Comment