Managing Files And Folders In PowerShell

Using Windows PowerShell, you can easily manage files and folders in your computer. Mainly, it is very helpful for Windows System Admins who frequently work with file system to perform tasks like create, copy, rename, and delete files and folders.

List all the files within a folder

To get all the items in a folder, use Get-ChildItem Cmdlet.

Example

PS C:\ > Get-ChildItem -Force C:\

Windows PowerShell

To List Everything (Hidden or System Items)

Adding "-Force" at the end of the above Cmdlet gives you all the items in a folder including hidden or system files.

Example

PS C:\> Get-ChildItem -Force C:\ -Recurse

Windows PowerShell

Create Folders

You can create new folders in PowerShell using the below Cmdlet. To Create a folder, specify the -ItemType as “directory”

Example

PS C:\> New-Item -Path ‘C:\Rakesh\New Folder’ -ItemType “directory”

Windows PowerShell

[ Note: In the above Cmdlet, I specified my folder location as C: \Rakesh\New Folder]

Create an empty text file

To create an empty text file, use the below Cmdlet. Specify the -ItemType as “file”

Example

PS C:\> New-Item -Path ‘C:\Rakesh\New Folder

\file.txt’ -ItemType “file”

Windows PowerShell

ZImportant Note: You need to specify the -ItemType as “file” to create an empty file. If you want create a folder, then the -ItemType should be “directory” ]

Copy File and Folders

To copy files and folders from one location to other location use the below syntax. You need to specify the Source and Destination paths here.

Example

PS C:\> Copy-Item -Path C:\Rakesh -Destination C:\Vemulawada

Windows PowerShell

[Note: In the above syntax, Rakesh and Vemulawada are names of the folders ]

OverWrite Existing File/Fiolder

If the destination file already exists, and you still try to copy, it will fail. You can use -Force parameter to overwrite the existing item.

Example

PS C:\> Copy-Item -Path C:\Rakesh -Destination C:\Vemulawada -Force

As you can see in the below screenshot, I am trying to copy the folder “Rakesh” into another folder “Vemulawada”, but “Rakesh” already exists in the destination, that’s why my copy attempt is failed.

In this situation, if you want to overwrite the files, use -Force parameter as shown below.

Windows PowerShell

Remove File or Folder

To Remove a file or folder, Use the below syntax.

[ Note : You need to Specify the path of the Item you want to delete ]

Example

PS C:\> Remove-Item C:\Rakesh

Windows PowerShell

Map a Local Folder as Accessible Local Drive

You can map a local folder to a local drive and access that as physical Windows drive. Use the “subst” command to achieve this. The example below will create a local drive (p) rooted in the local “ProgramFiles” folder.

Example

PS C:\> subst p: $env:ProgramFiles

Windows PowerShell

Once you run the above command, and go back to File Explorer, you will find a new local drive with alphabet “P” for “ProgramFiles” content. As you can see in the following screenshot -

Windows PowerShell

Image: subst created a new local drive Local Disk(P:)

Rename a file or folder

To rename a file or folder, use the below syntax.

Example The below command renames file3 to File4.

PS C:\> Remove-Item C:\file3 File4.txt

Windows PowerShell

Up Next
    Ebook Download
    View all
    Learn
    View all