The following articles provide a brief introduction to carrying out file system management tasks by using Windows PowerShell; these tasks include such things as creating, renaming, copying, and deleting files and folders. As is so often the case with Windows PowerShell, the code snippets found in these articles can either be included within a Windows PowerShell script or typed directly into the Windows PowerShell console.
Using the Copy-Item Cmdlet
Copying Files or Folders
Want to a copy a file or folder to a new location? Then you want the Copy-Item cmdlet. For example, here’s a command that copies the file Test.txt from the C:\Scripts folder to the C:\Test folder:Copy-Item c:\scripts\test.txt c:\test
Copy-Item c:\scripts\* c:\test
Copy-Item c:\scripts\*.txt c:\test
Copy-Item c:\scripts c:\test -recurse
Copy-Item Aliases |
---|
|
Using the New-Item Cmdlet
Creating a New File or Folder
New-Item is a quick and easy way to create a new file or folder on your computer. For example, suppose you want to create a new directory named Windows PowerShell within the C:\Scripts folder. To do that call New-Item along with: 1) the full path to the new folder; and, 2) the new item type (which you can specify using the -type parameter and the value directory). The command in question will look like this:New-Item c:\scripts\Windows PowerShell -type directory
New-Item c:\scripts\new_file.txt -type file
New-Item : The file 'C:\scripts\new_file.txt' already exists.
New-Item c:\scripts\new_file.txt -type file -force
And speaking of new, empty files, you can also use the -value parameter to add some data to your new file. This command adds the phrase This is text added to the file at the same time it creates New_file.txt:
New-Item c:\scripts\new_file.txt -type file -force -value "This is text added to the file"
New-Item Aliases |
---|
|
Using the Remove-Item Cmdlet
Deleting a File or Folder (Or Other Type of Object)
The Remove-Item cmdlet does exactly what the name implies: it enables you to get rid of things once and for all. Tired of the file C:\Scripts\Test.txt? Then delete it:Remove-Item c:\scripts\test.txt
Remove-Item c:\scripts\*
Confirm The item at C:\test\scripts 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"):
Remove-Item c:\scripts\* -recurse
Remove-Item c:\scripts\* -exclude *.wav
Remove-Item c:\scripts\* -include .wav,.mp3
And yes, if you want to get really fancy you can use both -include and -exclude in the same command. What do you suppose will happen when we run this command?
Remove-Item c:\scripts\* -include *.txt -exclude *test*
Incidentally, the Remove-Item cmdlet has a -whatif parameter that doesn’t actually remove anything but simply tells you what would happen if you did call Remove-Item. What do you mean that doesn’t make any sense? Here, take a look at this command:
Remove-Item c:\scripts\*.vbs -whatif
What if: Performing operation "Remove File" on Target "C:\scripts\imapi.vbs". What if: Performing operation "Remove File" on Target "C:\scripts\imapi2.vbs". What if: Performing operation "Remove File" on Target "C:\scripts\methods.vbs". What if: Performing operation "Remove File" on Target "C:\scripts\read-write.vbs ". What if: Performing operation "Remove File" on Target "C:\scripts\test.vbs". What if: Performing operation "Remove File" on Target "C:\scripts\winsat.vbs".
Remove-Item alias:\show
Set-Location env:\
Remove-Item Aliases |
---|
|
Using the Move-Item Cmdlet
Moving a File or Folder
You know how it is: as soon as you have everything set up just perfect, you’ll invariably want (or need) to change things. The files you put in the C:\Scripts folder? Turns out they should really be in C:\Test. Or maybe just the .zip files should be in C:\Test. Or maybe - well, you get the idea. Can you use Windows PowerShell to move items from one location or another? Let’s put it this way: if you couldn’t, it would be pretty silly to have a cmdlet named Move-Item.Let’s take a look at a very simple example: moving a single file from one folder to another. To do that, call Move-Item followed by, in order, the path to the file to be moved and the location to move it to. For example, this command moves the file C:\Scripts\Test.zip to C:\Test:
Move-Item c:\scripts\test.zip c:\test
Move-Item c:\scripts\*.zip c:\test
By default, Move-Item will not overwrite any existing files in the target folder. For example, suppose you’re trying to move a file named Test.zip from C:\Scripts to C:\Test. However, C:\Test already contains a file named Test.zip. In that case the move will fail … unless you include the -force parameter, which instructs Move-Item to overwrite existing files. This command will move Test.zip to the Test folder, even if the file C:\Test\Test.zip already exists:
Move-Item c:\scripts\test.zip c:\test -force
Here’s what the command looks like:
Move-Item c:\scripts\950.log c:\test\mylog.log
Move-Item Aliases |
---|
|
Using the Rename-Item Cmdlet
Renaming a File or Folder
What can you do with the Rename-Item cmdlet? Well, one thing you can do is rename files or folders; all you have to do is call Rename-Item followed by two parameters:- The current path for the file or folder.
- The new name for the file or folder.
Rename-Item c:\scripts\test.txt new_name.txt
Rename-Item Aliases |
---|
|
Using the Get-ChildItem Cmdlet
Replicating (and Extending) the DIR Command
In its basic form the Get-ChildItem cmdlet provides functionality similar to the dir command. For example, if you simply type Get-ChildItem at the Windows PowerShell prompt you’ll get back information about the objects in the current location:Directory: Microsoft.Windows PowerShell.Core\FileSystem::C:\Documents and Settings\kenmyer Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 3/1/2006 9:03 AM Bluetooth Software d---s 5/10/2006 8:55 AM Cookies d---- 5/9/2006 2:09 PM Desktop d-r-- 5/9/2006 8:22 AM Favorites d-r-- 5/9/2006 2:24 PM My Documents d-r-- 3/1/2006 8:15 AM Start Menu d---s 3/1/2006 3:41 PM UserData d---- 3/16/2006 3:29 PM WINDOWS
Get-ChildItem -recurse
Get-ChildItem env:
Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
Note. Get-ChildItem cannot be used to retrieve information about the registry values contained within a subkey. For that you need to use the Get-ItemProperty cmdlet. |
Get-ChildItem c:\scripts\*.* -include *.txt,*.log
Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 4/6/2006 8:28 PM 3508 950.Log -a--- 5/6/2006 10:06 AM 0 Employees.txt -a--- 5/6/2006 10:06 AM 0 Employees_NODUPL.txt -a--- 5/6/2006 10:06 AM 0 ExcelData.txt -a--- 3/3/2006 9:24 PM 14894 methods.txt -a--- 4/28/2006 1:36 PM 41 new_name.txt -a--- 3/7/2006 1:44 PM 4112 read-write.txt -a--- 4/11/2006 11:04 AM 18 servers.txt -a--- 5/5/2006 9:09 PM 53358 tee.txt -a--- 4/26/2006 12:28 PM 1125 temporary_print_file.txt -a--- 5/6/2006 10:30 PM 34184 test.log -a--- 5/9/2006 3:17 PM 58 test.txt -a--- 4/6/2006 10:26 PM 205 test_NODUPL.txt -a--- 4/28/2006 1:16 PM 27 x.txt -a--- 5/8/2006 2:39 PM 25 y.txt
Get-ChildItem c:\scripts\*.* -exclude *.txt,*.log
The information returned by Get-ChildItem can also be piped into the Sort-Object cmdlet, providing a way to sort the data by in some other format. Would you rather see files sorted by size (length) than by name? Then use this command:
Get-ChildItem c:\scripts\*.* | Sort-Object length
Get-ChildItem c:\scripts\*.* | Sort-Object length -descending
Get-ChildItem Aliases |
---|
|
Using the Get-Item Cmdlet
Retrieving a Specific Item
The Get-Item cmdlet makes it easy to retrieve a specific item (such as a file, a folder, or a registry key). Why would you want to do that? Well, for one thing, it makes it very easy to retrieve the properties of those items. For example, suppose you’d like to know the last time someone accessed the C:\scripts folder. Here’s a command that will retrieve that information:$(Get-Item c:\scripts).lastaccesstime
$(Get-Item hkcu:\software).subkeycount
Get-Item hkcu:\software | Get-Member
Get-Item Aliases |
---|
|
Using the Test-Path Cmdlet
Verifying the Existence of a File or Folder
One of the primary uses of Test-Path is to verify the existence of a file or folder. For example, this command checks to see whether the file C:\Scripts\Test.txt exists:Test-Path c:\scripts\test.txt
As is usually the case with cmdlets, you can use wildcards with Test-Path. For example, this script tells you whether or not there are any .wma files in C:\Scripts:
Test-Path c:\scripts\*.wma
Test-Path HKCU:\Software\Microsoft\Windows\CurrentVersion
No comments:
Post a Comment