MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
The file system provider is the easiest Windows PowerShell provider to understand-it provides access to the file system. When Windows PowerShell is launched, it automatically opens on the C:\PSDrive. Using the Windows PowerShell filesystem provider, you can create both directories and files. You can retrieve properties of files and directories, and you can delete them as well. In addition, you can open files and append or overwrite data to the files. This can be done with inline code, or by using the pipelining feature of Windows PowerShell. The commands used in the procedure are in the
Working with directory listings
-
Open Windows PowerShell.
-
Use the Get-ChildItem cmdlet to obtain a directory listing of the C:\ drive. Use the gci alias to reduce typing. This is shown here:
GCI C:\
-
Use the up arrow to retrieve the gci C:\ command. Pipeline the object created into a Where-Object cmdlet, and look for containers. This will reduce the output to only directories. The modified command is shown here:
GCI C:\ | where {$_.psiscontainer}
-
Use the up arrow to retrieve the gci C:\ | where {$_.psiscontainer} command and use the exclamation point (!), meaning not, to retrieve only items in the PSDrive that are not directories. The modified command is shown here:
GCI C:\ | where {!$_.psiscontainer}
-
This concludes this procedure. Do not close Windows PowerShell. Leave it open for the next procedure.
Identifying properties of directories
-
Use the Get-ChildItem cmdlet and supply a value of C:\ for the path argument. Pipeline the resulting object into the Get-Member cmdlet. Use the gci and gm aliases to reduce typing. This command is shown here:
GCI -Path C:\ | GM
-
The resulting output contains methods, properties, and more. Filter the output by pipelining the output into a Where-Object cmdlet and specifying the membertype attribute as equal to property. To do this, use the up arrow to retrieve the previous gci -path C:\ | gm command. Pipeline the resulting object into the Where-Object cmdlet and filter on the membertype attribute. The resulting command is shown here:
GCI -Path C:\ | GM | Where {$_.membertype -eq "property"}
-
The previous gci -path C:\ | gm | where {$_.membertype -eq "property"} command returns information on both the System.IO.DirectoryInfo and the System.IO.FileInfo objects. To reduce the output to only the properties associated with the System.IO.FileInfo object, we need to use a compound Where-Object cmdlet. Use the up arrow to retrieve the gci -path C:\ | gm | where {$_.membertype -eq "property"} command. Add the And conjunction and retrieve objects that have a typename that is like *file*. The modified command is shown here:
GCI -Path C:\ | GM | where {$_.membertype -eq "property" -AND $_.typename -like "*file*"}
-
The resulting output only contains the properties for a System.IO.FileInfo object. These properties are shown here:
TypeName: System.IO.FileInfo Name MemberType Definition ---- ---------- ---------- Attributes Property System.IO.FileAttributes Attributes {get;set;} CreationTime Property System.DateTime CreationTime {get;set;} CreationTimeUtc Property System.DateTime CreationTimeUtc {get;set;} Directory Property System.IO.DirectoryInfo Directory {get;} DirectoryName Property System.String DirectoryName {get;} Exists Property System.Boolean Exists {get;} Extension Property System.String Extension {get;} FullName Property System.String FullName {get;} IsReadOnly Property System.Boolean IsReadOnly {get;set;} LastAccessTime Property System.DateTime LastAccessTime {get;set;} LastAccessTimeUtc Property System.DateTime LastAccessTimeUtc {get;set;} LastWriteTime Property System.DateTime LastWriteTime {get;set;} LastWriteTimeUtc Property System.DateTime LastWriteTimeUtc {get;set;} Length Property System.Int64 Length {get;} Name Property System.String Name {get;}
-
This concludes this procedure. Do not close Windows PowerShell. Leave it open for the next procedure.
Creating folders and files
-
Use the Get-Item cmdlet to obtain a listing of files and folders. Pipeline the resulting object into the Where-Object cmdlet and use the PsIsContainer property to look for folders. Use the name property to find names that contain the word my in them. Use the gi alias and the where alias to reduce typing. The command is shown here:
GI * | Where {$_.PsisContainer -AND $_.name -Like "*my*"}
-
If you were following along in the previous chapters, you will have a folder called Mytest off the root of the C:\ drive. Use the Remove-Item cmdlet to remove the Mytest folder. Specify the recurse argument to also delete files contained in the C:\Mytest folder. If your location is still set to Env, then change it to C or search for C:\Mytest. The command is shown here:
RI mytest -recurse
-
Press the up arrow twice and retrieve the gi * | where {$_.PsisContainer -AND $_.name -Like "*my*"} command to confirm the folder was actually deleted. This command is shown here:
GI * | Where {$_.PsisContainer -AND $_.name -Like "*my*"}
-
Use the New-Item cmdlet to create a folder named Mytest. Use the path argument to specify the path of C:\. Use the name argument to specify the name of Mytest, and use the type argument to tell Windows PowerShell the new item will be a directory. This command is shown here:
New-Item -Path C:\ -Name mytest -Type directory
-
The resulting output, shown here, confirms the operation:
Directory: Microsoft.PowerShell.Core\FileSystem::C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 1/4/2007 2:43 AM mytest
-
Use the New-Item cmdlet to create an empty text file. To do this, use the up arrow and retrieve the previous new-item -path C:\ -name Mytest -type directory command. Edit the path argument so that it is pointing to the C:\Mytest directory. Edit the name argument to specify a text file named Myfile, and specify the type argument as file. The resulting command is shown here:
New-Item -Path C:\mytest -Name myfile.txt -type file
-
The resulting message, shown here, confirms the creation of the file:
Directory: Microsoft.PowerShell.Core\FileSystem::C:\mytest Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 1/4/2007 3:12 AM 0 myfile.txt
-
This concludes this procedure. Do not close Windows PowerShell. Leave it open for the next procedure.
Reading and writing for files
-
Delete Myfile.txt (created in the previous procedure). To do this, use the Remove-Item cmdlet and specify the path argument as C:\Mytest\Myfile.txt. This command is shown here:
RI -Path C:\mytest\myfile.txt
-
Use the up arrow twice to retrieve the new-item -path C:\Mytest -name Myfile.txt -type file. Add the -value argument to the end of the command line, and supply a value of my file. This command is shown here:
New-Item -Path C:\mytest -Name myfile.txt -Type file -Value "My file"
-
Use the Get-Content cmdlet to read the contents of Myfile.txt. This command is shown here:
Get-Content C:\mytest\myfile.txt
-
Use the Add-Content cmdlet to add additional information to the Myfile.txt file. This command is shown here:
Add-Content C:\mytest\myfile.txt -Value "ADDITIONAL INFORMATION"
-
Press the up arrow twice and retrieve the get-content C:\Mytest\Myfile.txt command, which is shown here:
Get-Content C:\mytest\myfile.txt
-
The output from the get-content C:\Mytest\Myfile.txt command is shown here:
My fileADDITIONAL INFORMATION
-
Press the up arrow twice, and retrieve the add-content C:\mytest\Myfile.txt -value "ADDITIONAL INFORMATION" command to add additional information to the file. This command is shown here:
Add-Content C:\mytest\myfile.txt -Value "ADDITIONAL INFORMATION"
-
Use the up arrow to retrieve the get-content C:\Mytest\Myfile.txt command, which is shown here:
Get-Content C:\mytest\myfile.txt
-
The output produced is shown here. Notice that the second time, the "ADDITIONAL INFORMATION" command was added to a new line.
My fileADDITIONAL INFORMATION ADDITIONAL INFORMATION
-
Use the Set-Information cmdlet to overwrite the contents of the Myfile.txt file. Specify the value argument as "Setting information". This command is shown here:
Set-Content C:\mytest\myfile.txt -Value "Setting information"
-
Use the up arrow to retrieve the get-content C:\Mytest\Myfile.txt command, which is shown here:
Get-Content C:\mytest\myfile.txt
-
The output from the Get-Content command is shown here:
Setting information
-
This concludes this procedure.
Категории