PowerShell One-Liners for Managing the File System
Simple commands let you copy, move, rename, and delete files and folders
July 31, 2007
Executive Summary:
Windows PowerShell can help ease your Windows operating system (OS) file management. |
Windows PowerShell includes many built-in aliases for cmdlets to make writing code easier. |
Simple Windows PowerShell "one-liners" can be used to retrieve files and folders, copy them, move them, and create or delete them. |
If you’ve used the command prompt to manage files and folders on your Windows systems, you know how easy many file-management tasks can be. You’ll find you can perform many of these tasks just as easily by using Windows PowerShell. In fact, you can take advantage of the full scripting and pipelining capabilities of PowerShell to carry out your file-system operations.
Getting Started, Getting Help
As you move into PowerShell, you should familiarize yourself with the basic commands—the “one-liners”—that let you access and manipulate files and folders. The best place to begin is with one of the most basic file-system commands: retrieving the name of the current working directory. To do so, enter the following command at the PowerShell command prompt:
get-location
As Figure 1 shows, the get-location cmdlet retrieves the name of the working directory (C, in this example) and returns it to the PowerShell window.
You might want to access information about how a particular cmdlet works and the parameters it takes. You can use the get-help cmdlet to retrieve the Help file for any cmdlet. For example, to retrieve the Help file for the get-location cmdlet, enter
get-help get-location
Figure 2 shows the result of running this command. The get-help cmdlet comes in quite handy no matter how experienced you are with PowerShell. You can use the get-command cmdlet to retrieve a list of all available commands.
In addition to being able to determine the current working directory, you might want to change that directory. The following command changes the working directory to C:Program FilesMicrosoft Office :
set-location -path ` "c:program filesmicrosoft office"
The command uses the set-location cmdlet to set the directory and uses the -path parameter to specify the name of the new working directory. The back tick at the end of the first line lets PowerShell know that the command continues to a second line. On the second line, note that the -path parameter value is enclosed in quotes. Using quotes is necessary only if the value contains spaces. Also note that you don’t always need to include the name of the parameter; you can specify only the parameter value if that value is in the expected position. For example, the following command returns the same result as the preceding command:
set-location ` "c:program filesmicrosoft office"
Saving Time with Aliases
You might find that some of these cmdlet names are cumbersome to work with, especially when typing them repeatedly. Fortunately, PowerShell includes several built-in aliases that you can substitute for the cmdlet name. For example, you can use the following command to change the working directory back to the C root directory:
cd c:
The cd keyword is a built-in alias for the set-location cmdlet. Many cmdlets have associated aliases. For example, you can use the help alias to refer to the get-help cmdlet or the pwd alias to refer to the get-location cmdlet. To see a complete list of aliases, enter
gal | select name, definition
The statement uses the gal alias to refer to the get-alias cmdlet, which retrieves a list of aliases. The results are sent down the pipeline, represented by the pipe character (|), to the second command. That command uses the select alias to refer to the select-object cmdlet and specifies the name and definition properties for each alias. As Figure 3 shows, the command returns only the values associated with those properties.
You can also view a list of aliases assigned to a specific cmdlet:
gal | select name, definition | where ` {$_.definition -eq "set-location"}
This statement retrieves a list of aliases and pipes them to the select-object cmdlet, which then pipes its results to a where-object cmdlet (represented by the where alias). The where-object cmdlet includes an expression enclosed in curly brackets. The expression uses the $_ symbol to reference the current value in the pipeline. The definition property is specified by adding a period and the property name after $_. The expression then specifies that the property value must equal (-eq) the specified string (in this example, set-location). As Figure 4 shows, the statement returns only those aliases associated with the set-location cmdlet.
Basic File Manipulation
Now that you have an idea of how to use aliases, let’s look at how to retrieve a list of files in a folder. First, download and run the PS_OneLiners_FileSystem_Setup.ps1 script, which creates a folder named C:ArchivedFiles, populates it with eight files, and creates an empty folder named C:ArchivedFiles2006. (If you're uncertain of how to run a PowerShell script, see the "What Can I Do With Windows PowerShell?" Web page at http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/invoke-expression.mspx.) Then, enter the following command to return a list of the files in C:ArchivedFiles:
dir c:archivedfiles
The dir alias refers to the get-childitem cmdlet, which retrieves items in a collection. The collection in this case is the C:ArchivedFiles folder. As Figure 5 shows, the folder contains eight text files.
You can also use wildcards to restrict which files are returned. For example, the following command returns only files whose name begins with “2007”:
dir c:archivedfiles2007*
The asterisk indicates that zero or more characters can follow "2007" in the file name. Figure 6 shows the result of running this command.
Because you can't include an asterisk in a filename, there’s no problem using the asterisk as a wildcard in your commands. However, you can use brackets in filenames and brackets can also be wildcards, so you must take a different approach when specifying a bracket that’s part of the filename. First, escape the bracket by preceding it with two back ticks (``), then enclose the entire pathname in single quotes, as in the following example:
dir 'c:archivedfiles2006archive``[01*'
As Figure 7 shows, this command retrieves any file that begins with “2006archive[01” in the filename.
You can also use PowerShell to copy files from one location to another. The following command copies the files whose name begins with "2006" from the C:ArchivedFiles folder to the C:ArchivedFiles2006 folder:
copy c:archivedfiles2006* ` c:archivedfiles2006
The copy alias refers to the copy-item cmdlet. The first parameter value specifies the source files and the second value specifies the destination. You can retrieve files from or copy files to a folder on a remote server:
copy c:archivedfiles2006* ` \server05carchivedfiles2006
The second argument contains the path to a folder on server05. You can also move files from one folder to another:
move c:archivedfiles2006* ` \server05carchivedfiles2006
The move alias refers to the move-item cmdlet. The above command moves all files that begin with “2006” to the ArchivedFiles2006 folder on server05.
PowerShell one-liners are handy for creating files and folders as well. For example, the following command creates a folder name ArchivedFiles on the C share of server05:
ni \server05cArchivedFiles ` -type directory
The ni alias refers to the new-item cmdlet, and the -type parameter specifies that the new item will be a directory. Here's how to use the new-item cmdlet to create an empty file:
ni \server05carchivedfiles2006Archives.txt ` -type file
This time the command creates a file (-type file) named 2006Archives.txt in the ArchivedFiles directory.
To rename a file, you can use a command such as
ren \server05carchivedfiles2006Archives.txt ` 2006Archives_complete.txt
The ren alias refers to the rename-item cmdlet. The first parameter specifies the full pathname of the original file, and the second parameter specifies the new filename.
To delete one or more files, you'd use a one-liner such as
del \server05carchivedfiles*.txt
The del alias refers to the remove-item cmdlet, and the parameter specifies the directory and filename. This cmdlet removes all files (as indicated by the asterisk wildcard) that have a .txt extension. To delete the folder itself, enter
del \server05carchivedfiles
Retrieving and Adding Content to Files
That’s all there is to copying, moving, renaming, and deleting files and folders. But you can also use PowerShell to retrieve the content of files. The following command retrieves the content of the 2005archives.txt file:
gc \server05carchivedfiles2005archives.txt
The command uses the gc alias to refer to the get-content cmdlet, and the parameter specifies the path and name of the file. Figure 8 shows the content that this sample command returns.
You can use a wildcard to retrieve the contents of multiple files. The asterisk in the command
gc \server05carchivedfiles*.txt
specifies that the content of all text files should be returned. As Figure 9 shows, this command returns the complete content from each of three files.
It’s important to know how the get-content cmdlet returns data. By default, the cmdlet returns each line in a file as a string that's part of a collection. Consequently, you can use string methods to modify the returned data. To view the methods available to the collection of strings returned from the 2004archives.txt file, enter
gc \server05carchivedfiles2004archives.txt ` | gm -memberType method
After the get-content cmdlet retrieves the content of the file, the results are piped to the get-member cmdlet (represented by the gm alias), which retrieves the object’s members. In this case, the get-member cmdlet uses the -memberType parameter to return only methods. Figure 10 shows the result of this command.
You can even add content to new or existing files. For example, the following command creates a file named 2004archives.txt and adds content to the file:
sc \server05carchivedfiles2004archives.txt ` -value "This is the content for the 2004archives.txt file."
The sc alias refers to the set-content cmdlet. The first parameter specifies the path and name of the new file. The second parameter (-value) specifies the string value that's added to the file.
The set-content cmdlet also lets you replace the content in an existing file. For example, the command
sc \server05carchivedfiles2004archives.txt ` -value "This is the new, updated content " ` + "for the 2004archives.txt file."
replaces all text in the original file with the new text. Note the use of the plus sign (+) on the third line. When a string continues to a new line, you need to treat the parts as separate strings and use PowerShell's concatenation operator (+) to join them.
The Power of One-Liners
As these sample one-liners demonstrate, you can use PowerShell to perform numerous file- and folder-related tasks. As you become comfortable with these basic skills, you’ll be able to incorporate them into other, more complex tasks. And you’ll find that many of the PowerShell principles you learn here also apply to other areas, helpingto make PowerShell a key resource in your administrative toolbox.
About the Author
You May Also Like