EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Delete File
Secondary 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 Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • 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 Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • 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

PowerShell Delete File

By Priya PedamkarPriya Pedamkar

PowerShell Delete File

Introduction to PowerShell Delete File

In PowerShell Delete File, to delete items in a PowerShell the Remove-Item cmdlet can be used. This cmdlet is used not only to remove files but also folders, functions, registry keys, etc. One of the most important works of a system administrator is to ensure that there is enough space on the drive and this process is referred to as housekeeping. These kinds of activities can be automated using a script that will delete files based on certain conditions. This article will cover in detail how the files can be deleted using Remove-Item cmdlet and how it can be automated.

Syntax

Below is the syntax of PowerShell Delete File:

Remove-Item[-Path] <String[]>[-Filter <String>][-Include <String[]>][-Exclude <String[]>][-Recurse][-Force] [-Credential <PSCredential>][-WhatIf][-Confirm][-Stream <String[]>] [<CommonParameters>] Remove-Item-LiteralPath <String[]>[-Filter <String>][-Include <String[]>][-Exclude <String[]>][-Recurse][-Force][-Credential <PSCredential>][-WhatIf] [-Confirm][-Stream <String[]>][<CommonParameters>]

Parameters

Below are the Parameters:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Confirm: An affirmation is appeared to the client before running the cmdlet. The information sort of this parameter is a switch. It is alluded by the assumed name cf. The default esteem is none. The parameter doesn’t acknowledge pipeline input and wild card characters are moreover not allowed.

Exclude: The values provided to this parameter will not be considered while executing the cmdlet. This is generally considered as the path parameter’s value. It allows wild card characters to be passed. This can be used only when the cmdlet is run using a wild card and not for a specific file or item. The information sort of this parameter is String[]. The default esteem is none. The parameter doesn’t acknowledge pipeline input whereas wild card characters are allowed.

Filter: This is used to filter the objects against which the cmdlet can be run. This is efficient because it applies the filter in the beginning rather than after fetching all the objects. The information sort of this parameter is String. The default esteem is none. The parameter doesn’t acknowledge pipeline input whereas wild card characters are allowed.

Force: Powers the cmdlet to evacuate things that cannot something else be changed, such as covered up or read-only records or read-only nom de plumes or factors. The cmdlet cannot evacuate variables and aliases which have constant values. The implementation differs based on the provider. Indeed, utilizing the Drive parameter, the cmdlet cannot abrogate security limitations. The information sort of this parameter is the switch. The default esteem is none. The parameter doesn’t acknowledge pipeline input and wild card characters are not permitted.

Include: The values provided to this parameter will only be considered while executing the cmdlet. This means the objects that are represented by this parameter only will not be deleted. This can be used only when the cmdlet is run using a wild card and not for a specific file or item. The information sort of this parameter is String[]. The default esteem is none. The parameter doesn’t acknowledge pipeline input whereas wild card characters are allowed.

LiteralPath: Indicates a way to one or more locations. The esteem of LiteralPath is utilized precisely because it is written. Single quotes are used to differentiate the escape sequences. The value of this parameter should be provided as to how it appears in its properties. The information sort of this parameter is String[]. It is alluded by the assumed namesPSPath and LP. The default esteem is none. The parameter acknowledges pipeline input and wild card characters are not allowed.

Path: This refers to the path in which the items to be deleted are present. The information sort of this parameter is String[]. The default esteem is none. The parameter acknowledges pipeline input and wild card characters are also allowed.

Recurse: If the specified path has some folders and subfolders, this denotes that those child items also need to be considered while performing the operation. This however doesn’t work well with include parameter which is a known one. The information sort of this parameter is a switch. The default esteem is none. The parameter doesn’t acknowledge pipeline input and wild card characters are also not allowed.

Stream: This parameter is only used within file system drives. The information sort of this parameter is a string[]. The default esteem is none. The parameter doesn’t acknowledge pipeline input whereas wild card characters are allowed.

Examples to Implement PowerShell Delete File

Below are the examples mentioned:

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,650 ratings)

Example #1

Code:

Write-Host "Deleting a single file"
#Remove-Item -Path "C:\Vignesh\Test\AD Group export to csv.txt"
Write-Host "File deleted" -ForegroundColor Green
Write-Host "delting files except some files of a particular type"
#Remove-Item -Path "C:\Vignesh\Test\*" -Exclude *.csv
Write-Host "Files other than csv files are deleted" -ForegroundColor Green
Write-Host "Deleting hidden file"
#Remove-Item -Path "C:\vignesh\Test\Path.csv" -Force
Write-Host "Hidden file is deleted" -ForegroundColor Green
Write-Host "Deleting files using recurse option"
#Remove-Item -Path "C:\vignesh\Test" -Recurse
Write-Host "All the files and folders are deleted successfully" -ForegroundColor Green
Write-Host "Removing specific items using include"
#Remove-Item -Path "C:\Vignesh\New folder\*" -Include *.png
Write-Host "Only the png files are deleted" -ForegroundColor Green

Output:

powershell delete file1

Example #2

Automating files to be deleted:

Code:

Write-Host"Deleting files greater than 60 days"
Get-ChildItem–Path"C:\logs"-Recurse|Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-60))} |Remove-Item
Write-Host"File deleted"-ForegroundColorGreen
Write-Host"Deleting files that are greater than 1000 mb"
$path="C:\vignesh\logs"
$allowedsize=1000
$fsize= (Get-ChildItem$path|Measure-Object-propertylength-sum)
$Sizeinmb="{0:N2}"-f ($fsize.sum /1MB) +"MB"
if ($Sizeinmb-ge$allowedsize) {
Get-ChildItem$path-Recurse|Remove-Item–Force
Write-Host"File deleted"-ForegroundColorGreen
}

Output:

powershell delete file2

Explanation: The above script deletes files that are older than 60 days and files that are greater than 1000 MB. The above script can be automated to run daily by creating a task in the task scheduler.

Conclusion

Thus, the article explained in detail about deleting files in PowerShell. It also explained the various scenarios of include and exclude that can be used while deleting files. It also showed how to automate the delete operation using a script and task scheduler. To learn more in detail it is advisable to write sample programs and practice them.

Recommended Articles

This is a guide to PowerShell Delete File. Here we discuss an introduction to PowerShell Delete File, syntax with parameters, examples. You can also go through our other related articles to learn more –

  1. PowerShell Modules
  2. Useful PowerShell Scripts
  3. PowerShell Copy-Item
  4. PowerShell Move-Item
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
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

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*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 Login

Forgot Password?

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.

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.

Let’s Get Started

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more