EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PowerShell Remove-Item

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PowerShell Tutorial » PowerShell Remove-Item

powershell-remove-item

Introduction to PowerShell Remove-Item

Removing a file is very common to work in our life, so we all are deleting some files and data daily, almost all programming languages provide remove file feature, but the good thing about PowerShell that it can remove many things other than files, like registry, alias and functions also. If we delete any system files many times all related files are not deleted, so with the help of PowerShell  Remove-Item we can even delete related files also. It can delete any large file with better speed along with deleting all its related files. We will discuss in this topic about syntax and uses along with base practices.

Syntax

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

A very simple syntax for PowerShell Remove-Item is

Remove-Item {-path}-optional “sourcePath/sourceFile”-{force,include,exclude,Recurse,whatif...}

Below syntax is cover regular use things,

Remove-Item
[-Path(source folder location)] <array of string>-optional
[-Force]-optional
[-Filter <String(filter contents for delete)>]-optional
[-Include <array of string>](specify contents need to be included like *.pdf,*.txt)-optional
[-Exclude <array of string>](specify contents need to be excluded like *txt ,*pdf)-optional
[-Recurse](this commands allow to delete folder containing another folder folder)-optional
[-WhatIf](It shows what can happen if command run)-optional
[-Confirm](before  deleting any file ask for confirmation)-optional
[<CommonParameters>]

Parameters

Here are the following parameters of PowerShell Remove-Item mention below:

  • Confirm: It is possible that by mistake we can run delete command and we know a delete command can do very blunder, so for such type of situation Confirm will be very useful as it will ask you once before deleting.
  • Exclude: This command takes an array, so whichever item you wanted to exclude you can mention in these commands. We can also use the wildcard in exclude command, like “*txt”                  
  • Filter: Here we can specify filter parameters for Remove-item command.
  • Force: Many times when the file is open we could not be able to delete it, so by using the command -Force we can delete it. Here it will give access to remove those items which are not allowed to remove, for example, if there is any file with read-only access and we wanted to remove this file then we can use this command. Or take another example if the file already open and we want to delete it than we can use -Force.
  • Include: Include is performed a similar task as -exclude, the only difference is here can define wildcard for files to be included at the time of removing file or media.      
  • LiteralPath: This command can delete the file on one or location in array format we can define them in an array of string format, we should use this command very carefully.                   
  • Path: It is the path from where we are going to delete an item. This -path command also allows wildcard.
  • Recurse: Here it Deletes folder containing folder inside it like we are removing a folder called /Ranjan and it contains three more folders /job  /education  /locations and all these folders contain some more folders inside it, which means child folder inside the child. So to delete this kind of file system we use -Recurse commands.
  • WhatIf: This only shows what could happen if the command runs, or in simple terms, it describes the command outcome.

Examples to Implement Power Shell Remove-Item

Here are the Examples for implementation of Power Shell Remove-Item.

Example #1

Let us delete files without any contents inside it. Here we are simply deleting a folder that contains nothing inside it. An example screen is given below.

Remove-Item ./test1/

Delete all files which include “-” in their names from test folders. It’s a very useful command as here we are deleting all the files which contain the name “-”.We can see in the Image below initially folder test contains 4 files out of the two files “ranjan-pandey.txt” and “suman-kumar.txt” contains “-”. So on running the command below it will delete these two files.

Popular Course in this category
Sale
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)41 Online Courses | 13 Hands-on Projects | 322+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (9,067 ratings)
Course Price

View Course

Related Courses
Shell Scripting Training (4 Courses, 1 Project)All in One Data Science Bundle (360+ Courses, 50+ projects)Data Visualization Training (15 Courses, 5+ Projects)

Remove-Item ./test/*-*

Output:

PowerShell Remove item example 1

Example #2

Let us do some recursive delete, in this, we are going to delete a folder(directory) and this folder also contains a subdirectory within the folder itself.

Remove-Item ./ranjan1/ -Recurse

Output:

PowerShell Remove item example 2

In the above example folder “ranjan1” contains “ranjan2” and “test.txt” file and again “ranjan2” contains a “test2.txt” now to delete the ranjan1 folder we need to write command  -Recurse along with Remove-Item.

Example #3

Many times some hidden files left even after deleting all contents, so to perform a complete delete we can use “-Force”. Let us one example of command  -Force Remove, This command used when we have some hidden files, so to delete hidden files we can use force.

Remove-Item -path ./test/ -Force

Output:

PowerShell Remove item example 3

In this example -Force will delete all files along with if any hidden file from the test.

Example #4

whatIf command, this command display what will happen on the execution of the command, please see the image below.

Remove-Item ./ranjan1 -whatif

Output:

PowerShell Remove item example 4

Example #5

In this example we are covering “-Confirm”.We are trying to delete a folder Jobs with passing -Confirm command here. Once we run this command it asked for if you want to delete it, and once we said “yes” it shows error file already exists, You can understand the utility of -Confirm command here as, file will be deleted only if you agree even after running the command, means very less possibility of making any mistakes.

Delete-Item -path ./source/ -Confirm

Output:

example 5

Example #6

let me explain to you one more very useful example here, suppose you want to delete all files from the folder with the specific extension, like “.txt”,”.xls”,”.pdf”.

Remove-Item -Path ./source/* -Include *.txt

Output:

 example 6

In the above example we are deleting all the files having “.txt” as the extension . in the same you can try to delete “.pdp” and “.config” extensions. These commands are very useful when we have a very huge amount of file and we wanted to delete some specific with a specific extension.

Conclusion – PowerShell Remove-Item

PowerShell Remove-Item command is a very useful command it plays a very pivotal role when we have a very huge amount of file inside the file system. When we have to delete the file with huge subfolders, it provides various types of supporting commands like -Force, Include and many others to perform its special duties.

Recommended Articles

This is a guide to PowerShell Remove-Item. Here we discuss the parameters of Power Shell Remove-Item along with the respective examples. You may also look at the following article to learn more –

  1. PowerShell Commands
  2. PowerShell String Functions
  3. How To Install PowerShell
  4. PowerShell Operators
  5. PowerShell Move-Item | Parameters and Examples
  6. Examples of PowerShell Date in Detail

Programming Languages Training (41 Courses, 13+ Projects)

41 Online Courses

13 Hands-on Projects

322+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
PowerShell Tutorial
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Basics
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Special Offer - Programming Languages Training (41 Courses, 13+ Projects) Learn More