Working with Files and Folders

    PowerShell  – Working with Files and Folders

    Creating Folders and Files:

    We use the cmdlet New-Item to create a folder or a file. We need to specify the Item type (the FileSystem Windows PowerShell provider distinguishes between directories and files).

    The following command creates a new folder in the present working directory.

    PS C:\temp> New-Item folder1 -ItemType Directory
     
        Directory: C:\temp
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----         1/7/2018  12:24 AM                folder1

    We can also create a folder or file directly by giving a path.

    PS C:\temp> New-Item c:\temp\folder2 -ItemType Directory
     
        Directory: C:\temp
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----         1/7/2018  12:28 AM                folder2
    
    PS C:\Users\Venu> New-Item c:\temp\folder2\textfile.txt -ItemType File
     
        Directory: C:\temp\folder2
     
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----         1/7/2018   6:48 PM              0 textfile.txt

    The above command will create a file textfile.txt in the given path C:\temp\folder2\

    Listing all files and folders in a folder:

    You can get all items directly within a folder by using Get-ChildItem. By default, Get-ChildItem does not display system files and hidden files. If you want to include those files, add an optional parameter Force. If you want to list files and folders of sub folders as well, add Recurse option.

    Get-ChildItem

    Get-ChildItem C:\temp\

    Get-ChildItem C:\Windows\System32\ -Force -Recurse

    Copying Files and Folders:

    Copying is done with Copy-Item. 

    Copy-Item C:\temp\folder2\textfile.txt c:\temp\folder1\

    If file is there already in the destination folder, you can use -Force option. This will replace the existing file.

    Copy-Item C:\temp\folder2\textfile.txt c:\temp\folder1\ -Force

    The following command will copy all content in the folder to new path.

    Copy-Item C:\temp C:\temp2 -Recurse -Force

    Removing All Files and Folders Within a Folder

    You can remove contained items using Remove-Item. It will prompt if there is any content in the folder.

    Remove-Item C:\temp2

    Remove-Item C:\temp2 -Recurse -Force

    Reading Text Files:

    Get-Content is used to read the content of a file into an array.

    Let is create a text file for testing.

     

    PS C:\temp> New-Item test.txt -ItemType File
     
     
        Directory: C:\temp
     
     
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    -a----         1/7/2018   7:19 PM              0 test.txt

    Add some content to the file

    PS C:\temp> Add-Content .\test.txt "This is first line"
    PS C:\temp> Add-Content .\test.txt "This is second line"
    PS C:\temp> Add-Content .\test.txt "third line"
    PS C:\temp> Add-Content .\test.txt "fourth line"
    PS C:\temp> Add-Content .\test.txt "fifth line"

    Now read content of the file using Get-Content

    PS C:\temp> Get-Content .\test.txt
    This is first line
    This is second line
    third line
    fourth line
    fifth line
    PS C:\temp>

    We can also assign the output to a variable. Result will be stored as an array. If you want to display first line only, try the following:

    PS C:\temp> $content = Get-Content .\test.txt
    PS C:\temp> $content[0]
    This is first line

     

     


    © 2019 WinAdmin.org . All Rights Reserved.
    Cookies make it easier for us to provide you with our services. With the usage of our services you permit us to use cookies.
    Ok Decline