File and Folder Management with PowerShell

In this post, we will only focus on PowerShell cmdlets (commands) that uses to create, copy, move, delete files and folders etc. Let’s start with file creation cmdlet in PowerShell.


Create File-

New-Item cmdlet (command) is used to create file. Below command creates a text file named "Test.txt" in the “Data” folder of C: drive. 

New-Item -path "C:\Data\" -Name "Test.txt" -ItemType "File"

Or you can also create a file without using -Path and –ItemType parameters as these parameters are optional.

New-Item C:\Data\Test.txt

You can create multiple files at a time using below PowerShell command. Comma (,) is mandatory between each file path but quotation mark (“) is optional.

New-Item -path "C:\Data\test1.txt", "C:\Data\Text2.txt" -ItemType File

Write content to text file-

Set-Content cmdlet is used with “–value” parameter to write new content or replace content in a file. “-Value” parameter is optional, but double quotation (“) or single quotation is mandatory.

Set-Content -Path C:\Data\Test.txt -value 'Hello Friends'

Append content to specified file-

Add-Content cmdlet append content in the file instead of overwriting file.

Add-Content -Path C:\Data\Test.txt -value 'Welcome'

Get content of specified file-

Get-content cmdlet gets the content of the item at the location specified by the path

Get-Content -Path C:\Data\Test.txt

Below command display the first 10 lines of file in the PowerShell console using “-TotalCount” parameter

Get-Content -Path C:\Data\Test.txt –TotalCount 10

Delete content of file-

Clear-content cmdlet is used to delete content of file. In the below example, it will erase all the content of Test file

Clear-Content -Path C:\Data\Test.txt

Copy file-

Copy-Item is used to copy a file to the specified directory

Copy-Item -Path C:\Data\Test.txt -Destination C:\Backup

Move file-

Move-Item is used to move a file to the specified directory

Move-Item -Path C:\Data\Test.txt -Destination C:\Backup 

Delete file-

In the below example, it will delete Test named file

Remove-Item -Path C:\Data\Test.txt


Create folder/directory-

New-Item cmdlet (command) is also used to create folder. In the below example, Data1 folder will be created in C: drive.

New-Item -Path "C:\" -Name "Data1" -ItemType "Directory"

Or (without -Name parameter)

New-Item -Path "C:\Data1" -ItemType "Directory"

Or (without -Path and -ItemType parameters) 

New-Item C:\Data1 

You can also create multiple folders at a time using below command. Comma (,) between folders and -ItemType are mandatory but quotation mark (“) is optional.

New-Item -Path "C:\Data2", "C:\Data3", "C:\Data4" -ItemType “Directory”

if the location is same for all folders that’s you want to create, then no need to mention full path each time. 

New-Item -Path C:\Data2, Data3, Data4 -ItemType “Directory”

Get information about files and folder 

Get-ChildItem cmdlet is used to get information about files and folders which present on specific one or more locations.

Below command display only information about files and folders that exist on C:\Data folder location. 

Get-ChildItem -Path C:\Data

But above command won’t show any files and folders under subfolder. For that you need to use “–Recurse” parameter to display information about files and folders under subfolder.

Get-ChildItem -Path C:\Data –Recurse

Use “–force” parameter to get hidden file information

Get-ChildItem -Path C:\Data –Recurse –Force

Copy folder/directory-

Copy-Item cmdlet is used to copy folder from one location to another location. User "-Recurse" parameter with Copy-Item command to copy folder and it's content otherwise it copy only main folder. 

Below command copies "Data" folder and its contents to C:\Backup location.

Copy-Item -Path C:\Data -Destination C:\Backup -Recurse

IF you want to copy only files and subfolders not the main folder, then use wildcard (*) in the source path as per below command.

Copy-Item -Path C:\Data\* -Destination C:\Backup –Recurse

Use "-Force" parameter to overwrite folder if folder is exist with the same name in destination location.

Copy-Item -Path C:\Data\* -Destination C:\Backup –Recurse -Force

Move folder/directory-

Move-Item command is used to move folder and its content from one location to another location. In the below example, Data folder and its content copies to C:\Backup folder path

Move-Item -Path C:\Data -Destination C:\Backup

Delete folder

Remove-Item command is used to delete folder and its content.

Remove-Item -Path C:\Data



No comments:

Post a Comment