|
|
DETAILED DESCRIPTIONThe Windows PowerShell FileSystem Provider lets you get, add, change, clear, and delete files and directories in Windows PowerShell.
The FileSystem Provider exposes Windows PowerShell drives that correspond to the logical drives configured on your computer, including drives mapped to network shares. For example, a computer with one floppy disk drive, one hard disk drive, and one mapped network shared directory might have drives named A, C, and Z. The FileSystem Provider exposes Windows PowerShell drives that correspond directly to A, C, and Z, allowing you to reference these drives from within Windows PowerShell. For example, to reference drive C, you use C:, as shown in the following example:
The command returns all the contents on the C drive, including files and directories. When you reference a specific directory or file through the FileSystem Provider, you must provide the information necessary to identify that directory or file. This means that, in some cases, you must provide a fully qualified name. A fully qualified name includes the drive name (along with a colon), any directory and subdirectory names, and the file name (when applicable). For instance, the following example shows the fully qualified name for the Shell.dll file, which is located in the System32 subdirectory of the Windows directory on the C drive:
c:\windows\system32\shell.dll
As you can see, each element of the fully qualified name is separated by a backslash (\). Windows PowerShell also allows you to use a forward slash (/) to be consistent with a variety of other shells.
In some cases, you do not need to supply a fully-qualified name when referencing a file or directory. For example, if you want to access a file in your current working location, you need to provide only the file name. If your current working location is c:\windows, you can view a list of all the .dll files in that directory by using the following command:
If your working directory is something other than c:\windows, such as c:\program files\Windows PowerShell, your command might need to include the fully qualified name:
- Get-ChildItem c:\windows\*.dll
复制代码
In some cases, you can use relative references to a location. If your working location is c:\windows, and you want to view a list of .dll files in the c:\windows\system32 directory, you can use the following command:
- Get-ChildItem .\system32\*.dll
复制代码
The period before \system32 represents the current working location.
In some situations, your current working location will be on a drive other than a FileSystem drive. If this is the case, you must always include the name of the target drive in your reference. For example, suppose that your current working location is the env: drive. To view the contents of the C drive, you would use the following command:
EXAMPLESNavigating the File System
-------------------------- EXAMPLE 1 --------------------------
This command gets the current location:
The Get-Location cmdlet includes the functionality of commands like the cd command in the Windows Command Prompt and the pwd command in UNIX. For more information, type: get-help get-location
-------------------------- EXAMPLE 2 --------------------------
This command sets the current location:
Getting File and Directory Information
-------------------------- EXAMPLE 1 --------------------------
This command gets all the files and directories in the current directory:
By default, the Get-ChildItem cmdlet does not recurse. If files and folders are present in the current directory when you run this command, a System.IO.FileInfo object and a System.IO.DirectoryInfo object are returned.
-------------------------- EXAMPLE 2 --------------------------
This command gets all the files and directories in the current directory by using Get-ChildItem:
- get-childitem | where-object {!$_.psiscontainer}
复制代码
It pipes the results to Where-Object, which examines the PSIsContainer property and lets only the objects that are not (!) containers through the pipeline.
-------------------------- EXAMPLE 3 --------------------------
This command gets all the files and directories in the current directory by using Get-ChildItem. It pipes the results to Where-Object, which examines the PSIsContainer property and lets only the objects that are containers through the pipeline.
- get-childitem | where-object {$_.psiscontainer}
复制代码
-------------------------- EXAMPLE 4 --------------------------
This command gets all the files and directories in the current directory by using Get-ChildItem:
- get-item -path a | format-list *
复制代码
It pipes the results to the Where-Object cmdlet, which examines the PSIsContainer property and lets only the objects that are containers through the pipeline.
-------------------------- EXAMPLE 5 --------------------------
This command uses the Get-Item cmdlet to get information about the Test.txt file:
- get-item -path test.txt | format-list *
复制代码
The Format-List cmdlet is used to display all the properties of the resulting object.
Copying Files and Directories
-------------------------- EXAMPLE 1 --------------------------
This command copies the A.txt file from the C:\A directory to the C:\A\Bb directory:
- copy-item -path C:\a\a.txt -destination C:\a\bb\a.txt
复制代码
It overwrites files in the destination directory without prompting for confirmation.
-------------------------- EXAMPLE 2 --------------------------
This command copies all the files in the C:\A\Bb directory that have the .txt file name extension to the C:\A\Cc\Ccc\ directory:
- copy-item -path C:\a\bb\*.txt -destination C:\a\cc\ccc\
复制代码
It uses the original names of the files. The command overwrites the existing files in the destination directory without prompting for confirmation.
-------------------------- EXAMPLE 3 --------------------------
Copies all the directories and files in the C:\a directory to the C:\c directory. If any of the directories to copy already exist in the destination directory, the command will fail unless you specify the Force parameter.
- copy-item -path C:\a\* -destination C:\c -recurse
复制代码
Moving Files and Directories
-------------------------- EXAMPLE 1 --------------------------
This command moves the C.txt file in the C:\A directory to the C:\A\Aa directory:
- move-item -path C:\a\c.txt -destination C:\a\aa
复制代码
The command will not automatically overwrite an existing file that has the same name. To force the cmdlet to overwrite an existing file, specify the Force parameter.
-------------------------- EXAMPLE 2 --------------------------
This command moves the C:\A directory and all its contents to the C:\B directory:
- move-item -path C:\a -destination C:\b
复制代码
You cannot move a directory when that directory is the current location.
Managing File Content
-------------------------- EXAMPLE 1 --------------------------
This command appends the "test content" string to the Test.txt file:
- add-content -path test.txt -value "test content"
复制代码
The existing content in the Test.txt file is not deleted.
-------------------------- EXAMPLE 2 --------------------------
This command gets the contents of the Test.txt file and displays them in the console:
- get-content -path test.txt
复制代码
You can pipe the contents of the file to another cmdlet. For example, the following command reads the contents of the Test.txt file and then supplies them as input to the ConvertTo-HTML cmdlet:
- get-content -path test.txt | convertto-html
复制代码
-------------------------- EXAMPLE 3 --------------------------
This command replaces the contents of the Test.txt file with the "test content" string:
- set-content -path test.txt -value "test content"
复制代码
It overwrites the contents of Test.txt. You can use the Value parameter of the New-Item cmdlet to add content to a file when you create it.
|
|