Changes

Jump to: navigation, search

Working with File Systems in Windows PowerShell 1.0

3,086 bytes added, 15:11, 8 December 2008
Getting Information About Network Drives in PowerShell
-------- ------------ ---------
Z: \\Winserver-2\c 9408192512
</pre>
 
== Windows PowerShell File System Directory Listings ==
 
A listing of the files and sub-folders in a directory may be obtained using the ''Get-ChildItems'' cmdlet, or the ''dir'' alias> with no parameters, the current directory is assumed. Alternatively, a path may be provided to the desired directory:
 
<pre>
PS C:\Users\Administrator> get-childitem /tmp
 
 
Directory: Microsoft.PowerShell.Core\FileSystem::C:\tmp
 
 
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 12/5/2008 12:08 PM myfiles
</pre>
 
A recursive listing (which includes the contents of all sub-directories) is generated through the use of the ''-recurse'' option:
 
<pre>
PS C:\Users\Administrator> get-childitem /tmp -recurse
</pre>
 
In addition, the ''-filter'' option can be used to limit the listing to files and folders which match specified criteria:
 
<pre>
PS C:\Users\Administrator> get-childitem /tmp -filter *.txt
</pre>
 
The above example will list only files with a .txt filename extension. Similarly, multiple filtering criteria may be specified through the use of the ''-include'' option:
 
<pre>
PS C:\Users\Administrator> get-childitem /tmp -include *.txt,*.doc
</pre>
 
== Copying, Remaining and Deleting Files and Directories ==
 
The ''Copy-Item'' cmdlet (also available via the ''cp'' and ''copy'' aliases) allows files to be copied, and takes the source and destinations as parameters. For example, the following command copies the file ''C:\tmp\myfiles\test.txt'' to ''C:\tmp\test2.txt'':
 
<pre>
PS C:\Users\Administrator> copy-item c:\tmp\myfiles\test.txt c:\tmp\test2.txt
</pre>
 
Multiple copies may be made with a single command by using wildcards. For example, to copy the entire contents of one directory into another:
 
<pre>
PS C:\Users\Administrator> copy-item c:\tmp\myfiles\*.* c:\tmp
</pre>
 
The ''-recurse'' option performs a recursive copy whereby all sub-directories and folders are included in the copy operation:
 
<pre>
PS C:\Users\Administrator> copy-item -recurse c:\tmp c:\tmp2
</pre>
 
Files and directories are renamed and moved using the ''Rename-Item'' cmdlet, or the ''move'' or ''mv'' aliases:
 
<pre>
move-item c:\tmp\test2.txt c:\tmp\test3.txt
</pre>
 
Finally, files may be deleted using the ''Remove-Item'' cmdlet (also accessible as ''del'' and ''rm''):
 
<pre>
PS C:\Users\Administrator> remove-item c:\tmp\test3.txt
</pre>
 
If the item to be removed is a directory containing files and/or subdirectories, the cmdlet will prompt for verification that all contents of the directory are to be recursively removed as follows:
 
<pre>
 
PS C:\Users\Administrator> remove-item c:\tmp3
 
Confirm
The item at C:\tmp3 has children and the Recurse parameter was not specified. If you continue, all
children will be removed with the item. Are you sure you want to continue?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
</pre>
 
This prompt may be avoided by specifying the ''-recurse'' option:
 
<pre>
PS C:\Users\Administrator> remove-item -recurse c:\tmp3
</pre>

Navigation menu