Understanding File System Provider


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 image from book IdentifyingPropertiesOfDirectories.txt, image from book CreatingFoldersAndFiles.txt, and image from book ReadingAndWritingForFiles.txt files.

Working with directory listings

  1. Open Windows PowerShell.

  2. 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:\

  3. 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}

  4. 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}

  5. This concludes this procedure. Do not close Windows PowerShell. Leave it open for the next procedure.

Identifying properties of directories

  1. 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

  2. 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"}

  3. 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*"}

  4. 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;}

  5. This concludes this procedure. Do not close Windows PowerShell. Leave it open for the next procedure.

Creating folders and files

  1. 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*"}

  2. 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

  3. 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*"}

  4. 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

  5. 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

  6. 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

  7. 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

  8. This concludes this procedure. Do not close Windows PowerShell. Leave it open for the next procedure.

Reading and writing for files

  1. 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

  2. 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"

  3. Use the Get-Content cmdlet to read the contents of Myfile.txt. This command is shown here:

     Get-Content C:\mytest\myfile.txt

  4. 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"

  5. 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

  6. The output from the get-content C:\Mytest\Myfile.txt command is shown here:

     My fileADDITIONAL INFORMATION

  7. 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"

  8. Use the up arrow to retrieve the get-content C:\Mytest\Myfile.txt command, which is shown here:

     Get-Content C:\mytest\myfile.txt

  9. 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

  10. 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"

  11. Use the up arrow to retrieve the get-content C:\Mytest\Myfile.txt command, which is shown here:

     Get-Content C:\mytest\myfile.txt

  12. The output from the Get-Content command is shown here:

     Setting information

  13. This concludes this procedure.




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net