FileSystem

PROVIDER NAME
    FileSystem

DRIVES
    C, D

SYNOPSIS
    Provides access to files and directories.

DESCRIPTION
    The 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:

     Get-ChildItem c:

    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:

     Get-ChildItem *.dll

    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:

     Get-ChildItem c:

CAPABILITIES

TASKS
    TASK: Navigating the File System

        ————————– EXAMPLE 1 ————————–

        This command gets the current location:

        Get-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:

        Set-Location C:

    TASK: Getting File and Directory Information

        ————————– EXAMPLE 1 ————————–

        This command gets all the files and directories in the current directory:

        Get-ChildItem

        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.

    TASK: 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

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

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

    TASK: Managing Security Descriptors

        ————————– EXAMPLE 1 ————————–

        This command returns a System.Security.AccessControl.FileSecurity object:

        Get-Acl -path test.txt | Format-List -property *

        For more information about this object, pipe the command to the Get-Member cmdlet. Or, see “FileSecurity Class” in the MSDN (Microsoft Developer Network) library at http://go.microsoft.com/fwlink/?LinkId=145718.
        ————————– EXAMPLE 2 ————————–

        This command returns a System.Security.AccessControl.DirectorySecurity object:

        Get-Acl -path test_directory | Format-List -property *

        For more information about this object, pipe the command to the Get-Member cmdlet. Or, see “DirectorySecurity Class” in the MSDN library at http://go.microsoft.com/fwlink/?LinkId=145736.

    TASK: Creating Files and Directories

        ————————– EXAMPLE 1 ————————–

        This command creates the Logfiles directory on the C drive:

        New-Item -path c:\ -name logfiles -type directory

        ————————– EXAMPLE 2 ————————–

        This command creates the Log2.txt file in the C:\Logfiles directory and then adds the “test log” string to the file:

        New-Item -path c:\logfiles -name log.txt -type file

        ————————– EXAMPLE 3 ————————–

        Creates a file called Log2.txt in the C:\logfiles directory and adds the string “test log” to the file.

        New-Item -path c:\logfiles -name log2.txt -type file -value “test log”

    TASK: Renaming Files and Directories

        ————————– EXAMPLE 1 ————————–

        This command renames the A.txt file in the C:\A directory to B.txt:

        Rename-Item -path c:\a\a.txt -newname b.txt

        ————————– EXAMPLE 2 ————————–

        This command renames the C:\A\Cc directory to C:\A\Dd:

        Rename-Item -path c:\a\cc -newname dd

    TASK: Deleting Files and Directories

        ————————– EXAMPLE 1 ————————–

        This command deletes the Test.txt file in the current directory:

        Remove-Item -path test.txt

        ————————– EXAMPLE 2 ————————–

        This command deletes all the files in the current directory that have the .xml file name extension:

        Remove-Item -path *.xml

    TASK: Starting a Program by Invoking an Associated File

        ————————– EXAMPLE 1 ————————–

        The first command uses the Get-Service cmdlet to get information about local services.

        It pipes the information to the Export-Csv cmdlet and then stores that information in the Services.csv file.

        The second command uses Invoke-Item to open the Services.csv file in the program associated with the .csv extension:

        Get-Service | Export-Csv -path services.csv

        Invoke-Item -path services.csv

DYNAMIC PARAMETERS
    -Encoding <Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding>

            Unknown
                The encoding type is unknown or invalid. The data can be treated as binary.

            String
                Uses the encoding type for a string.

            Unicode
                Encodes in UTF-16 format using the little-endian byte order.

            Byte
                Encodes a set of characters into a sequence of bytes.

            BigEndianUnicode
                Encodes in UTF-16 format using the big-endian byte order.

            UTF8
                Encodes in UTF-8 format.

            UTF7
                Encodes in UTF-7 format.

            ASCII
                Uses the encoding for the ASCII (7-bit) character set.

        Cmdlets Supported: Add-Content, Get-Content, Set-Content

    -Delimiter <System.String>
        Specifies the delimiter to use when reading the file. The default is “\n” (end of line).

        Cmdlets Supported: Get-Content

    -Wait <System.Management.Automation.SwitchParameter>
        Waits for content to be appended to the file. If content is appended, it returns the appended content. If the content has changed, it returns the entire file.

        When waiting, Get-Content checks the file once each second until you interrupt it, such as by pressing CTRL+C.

        Cmdlets Supported: Get-Content

NOTES

RELATED LINKS
    about_providers