SlideShare a Scribd company logo
How To Check File and Delete – PowerShell
i | P a g e
Table of Contents
Overview.......................................................................................................................................................1
Applies To..................................................................................................................................................1
Pre-Requisites ...........................................................................................................................................1
Current Execution Policy.......................................................................................................................1
PowerShell Script – Delete File .....................................................................................................................2
PowerShell Snippet...................................................................................................................................2
PowerShell Script – Result ....................................................................................................................2
How To Check File and Delete – PowerShell
1 | P a g e
Overview
In this post / snippet we will demonstrate, deleting a file on the system if the file exists.
1) If the file exists it will be deleted
2) If the file doesn’t exist a message box will be displayed
3) If the user doesn’t key any file, an error message will be displayed.
Applies To
Tested on Windows 10, Windows 2008 R2, Windows 2012.
Pre-Requisites
To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or
“Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.
Policy Type Purpose
Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned Only scripts signed by a trusted publisher can be run.
RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted No restrictions; all Windows PowerShell scripts can be run.
Current Execution Policy
To know the current run the PowerShell cmdlet; Get-ExecutionPolicy
To list execution policies that can be configured run the PowerShell cmdlet; Get-ExecutionPolicy -List
How To Check File and Delete – PowerShell
2 | P a g e
PowerShell Script – Delete File
This script read filename from user input, check if file exists and delete it.
PowerShell Snippet
Clear-Host
#
# Read filename
#
$ReadFileName=Read-Host -Prompt "Enter file to be deleted"
if ($ReadFileName -eq "") {
Write-Host "No Filename Issued..."
[console]::beep(900,300)
[System.Windows.MessageBox]::Show($ReadFileName + ' - No Filename Issued ', "Delete File Status")
return
}
if (Test-Path $ReadFileName) {
del $ReadFileName
}
else {
Write-Host $ReadFileName does not exists!
[console]::beep(900,300)
[System.Windows.MessageBox]::Show($ReadFileName + ' - File does not exists.', "Delete File Status")
}
PowerShell Script – Result
When the script is executed, if the file exists file will be deleted or an error message will be shown.

More Related Content

What's hot (20)

PDF
How To Protect SSH Access with Fail2Ban on RHEL 7
VCP Muthukrishna
 
PDF
How To Configure SNMP Logging on RHEL 7
VCP Muthukrishna
 
PDF
How To Disable IE Enhanced Security Windows PowerShell
VCP Muthukrishna
 
PDF
How to Upgrade Openfire on CentOS 7
VCP Muthukrishna
 
PDF
Nginx bind() to 0.0.0.0:9080 failed
VCP Muthukrishna
 
PDF
How To Install and Configure GNome on CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Configure AWS CLI on RHEL 7
VCP Muthukrishna
 
PDF
How To Connect Amazon AWS EC2 with Key Pair – Linux
VCP Muthukrishna
 
PDF
How To Install and Configure Apache SSL on CentOS 7
VCP Muthukrishna
 
PDF
How To Install and Configure Chrony on RHEL 7
VCP Muthukrishna
 
PDF
How To Audit Server Login and Shutdown or Reboot Activity
VCP Muthukrishna
 
PDF
How to Install MariaDB Server or MySQL Server on CentOS 7
VCP Muthukrishna
 
PDF
How To Configure Apache VirtualHost on RHEL 7 on AWS
VCP Muthukrishna
 
PDF
VMWare Tools Installation and Troubleshooting Guide
VCP Muthukrishna
 
PDF
How To List Nginx Modules Installed / Complied on CentOS 7
VCP Muthukrishna
 
PDF
How to Install Configure and Use sysstat utils on RHEL 7
VCP Muthukrishna
 
PDF
How To Install and Generate Audit Reports in CentOS 7 or RHEL 7
VCP Muthukrishna
 
PDF
How To Install and Configure Screen on CentOS 7
VCP Muthukrishna
 
PDF
How To Install OpenFire in CentOS 7
VCP Muthukrishna
 
PDF
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
VCP Muthukrishna
 
How To Protect SSH Access with Fail2Ban on RHEL 7
VCP Muthukrishna
 
How To Configure SNMP Logging on RHEL 7
VCP Muthukrishna
 
How To Disable IE Enhanced Security Windows PowerShell
VCP Muthukrishna
 
How to Upgrade Openfire on CentOS 7
VCP Muthukrishna
 
Nginx bind() to 0.0.0.0:9080 failed
VCP Muthukrishna
 
How To Install and Configure GNome on CentOS 7
VCP Muthukrishna
 
How To Install and Configure AWS CLI on RHEL 7
VCP Muthukrishna
 
How To Connect Amazon AWS EC2 with Key Pair – Linux
VCP Muthukrishna
 
How To Install and Configure Apache SSL on CentOS 7
VCP Muthukrishna
 
How To Install and Configure Chrony on RHEL 7
VCP Muthukrishna
 
How To Audit Server Login and Shutdown or Reboot Activity
VCP Muthukrishna
 
How to Install MariaDB Server or MySQL Server on CentOS 7
VCP Muthukrishna
 
How To Configure Apache VirtualHost on RHEL 7 on AWS
VCP Muthukrishna
 
VMWare Tools Installation and Troubleshooting Guide
VCP Muthukrishna
 
How To List Nginx Modules Installed / Complied on CentOS 7
VCP Muthukrishna
 
How to Install Configure and Use sysstat utils on RHEL 7
VCP Muthukrishna
 
How To Install and Generate Audit Reports in CentOS 7 or RHEL 7
VCP Muthukrishna
 
How To Install and Configure Screen on CentOS 7
VCP Muthukrishna
 
How To Install OpenFire in CentOS 7
VCP Muthukrishna
 
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
VCP Muthukrishna
 

Similar to How To Check file exists and Delete PowerShell (20)

PDF
How To Check and Delete a File via PowerShell
VCP Muthukrishna
 
PDF
How To List Files and Display In HTML Format
VCP Muthukrishna
 
PPTX
Power shell training
David Brabant
 
PDF
One man loves powershell once he failed
Kazuhiro Matsushima
 
PPTX
Power Shell for System Admins - By Kaustubh
Kaustubh Kumar
 
PDF
How To List Files on Remote Server - PowerShell
VCP Muthukrishna
 
PDF
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
Hitesh Mohapatra
 
PPT
PowerShell Core Skills (TechMentor Fall 2011)
Concentrated Technology
 
PPTX
Getting Started With PowerShell Scripting
Ravikanth Chaganti
 
PDF
Windows Powershell in Action 1st Edition Bruce G. Payette
moudenongui20
 
PDF
Windows Powershell in Action 1st Edition Bruce G. Payette
takabedembe
 
PDF
Windows Powershell in Action 1st Edition Bruce G. Payette
zahithovor
 
PPTX
PowerShell-1
Saravanan G
 
PPTX
PowerShell 101 - What is it and Why should YOU Care!
Thomas Lee
 
PPTX
PowerShell 101
Thomas Lee
 
PPTX
Introduction to PowerShell and getting started
Ravikanth Chaganti
 
PDF
Windows Powershell Step By Step 3rd Edition Wilson Ed
forsenqenan
 
PPTX
learn ps new advance for all powershell.pptx
Ronnie Kapoor
 
PPTX
PowerShell 101
Thomas Lee
 
PDF
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
ClapperboardCinemaPV
 
How To Check and Delete a File via PowerShell
VCP Muthukrishna
 
How To List Files and Display In HTML Format
VCP Muthukrishna
 
Power shell training
David Brabant
 
One man loves powershell once he failed
Kazuhiro Matsushima
 
Power Shell for System Admins - By Kaustubh
Kaustubh Kumar
 
How To List Files on Remote Server - PowerShell
VCP Muthukrishna
 
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
Hitesh Mohapatra
 
PowerShell Core Skills (TechMentor Fall 2011)
Concentrated Technology
 
Getting Started With PowerShell Scripting
Ravikanth Chaganti
 
Windows Powershell in Action 1st Edition Bruce G. Payette
moudenongui20
 
Windows Powershell in Action 1st Edition Bruce G. Payette
takabedembe
 
Windows Powershell in Action 1st Edition Bruce G. Payette
zahithovor
 
PowerShell-1
Saravanan G
 
PowerShell 101 - What is it and Why should YOU Care!
Thomas Lee
 
PowerShell 101
Thomas Lee
 
Introduction to PowerShell and getting started
Ravikanth Chaganti
 
Windows Powershell Step By Step 3rd Edition Wilson Ed
forsenqenan
 
learn ps new advance for all powershell.pptx
Ronnie Kapoor
 
PowerShell 101
Thomas Lee
 
Learn Powershell Scripting Tutorial Full Course 1dollarcart.com.pdf
ClapperboardCinemaPV
 
Ad

More from VCP Muthukrishna (13)

PDF
How to Fix Duplicate Packages in YUM on CentOS 7
VCP Muthukrishna
 
PDF
How To Connect to Active Directory User Validation
VCP Muthukrishna
 
PDF
How To Connect To Active Directory PowerShell
VCP Muthukrishna
 
PDF
How To Setup SSH Keys on CentOS 7
VCP Muthukrishna
 
PDF
Windows PowerShell Basics - How To List PSDrive Info
VCP Muthukrishna
 
PDF
Windows PowerShell Basics – How To Create powershell for loop
VCP Muthukrishna
 
PDF
How To Construct IF and Else Conditional Statements
VCP Muthukrishna
 
PDF
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
VCP Muthukrishna
 
PDF
How To Create Power Shell Function Mandatory Parameter Value
VCP Muthukrishna
 
PDF
How To Create PowerShell Function
VCP Muthukrishna
 
PDF
How To Install and Configure Salt Master on Ubuntu
VCP Muthukrishna
 
PDF
How To Find Package Installation Date on RHEL 7
VCP Muthukrishna
 
PDF
How To Reset root Password on CentOS 7
VCP Muthukrishna
 
How to Fix Duplicate Packages in YUM on CentOS 7
VCP Muthukrishna
 
How To Connect to Active Directory User Validation
VCP Muthukrishna
 
How To Connect To Active Directory PowerShell
VCP Muthukrishna
 
How To Setup SSH Keys on CentOS 7
VCP Muthukrishna
 
Windows PowerShell Basics - How To List PSDrive Info
VCP Muthukrishna
 
Windows PowerShell Basics – How To Create powershell for loop
VCP Muthukrishna
 
How To Construct IF and Else Conditional Statements
VCP Muthukrishna
 
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
VCP Muthukrishna
 
How To Create Power Shell Function Mandatory Parameter Value
VCP Muthukrishna
 
How To Create PowerShell Function
VCP Muthukrishna
 
How To Install and Configure Salt Master on Ubuntu
VCP Muthukrishna
 
How To Find Package Installation Date on RHEL 7
VCP Muthukrishna
 
How To Reset root Password on CentOS 7
VCP Muthukrishna
 
Ad

Recently uploaded (20)

PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PPTX
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
PDF
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
99 Bottles of Trust on the Wall — Operational Principles for Trust in Cyber C...
treyka
 
Kubernetes - Architecture & Components.pdf
geethak285
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
Plugging AI into everything: Model Context Protocol Simplified.pdf
Abati Adewale
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 

How To Check file exists and Delete PowerShell

  • 1. How To Check File and Delete – PowerShell i | P a g e Table of Contents Overview.......................................................................................................................................................1 Applies To..................................................................................................................................................1 Pre-Requisites ...........................................................................................................................................1 Current Execution Policy.......................................................................................................................1 PowerShell Script – Delete File .....................................................................................................................2 PowerShell Snippet...................................................................................................................................2 PowerShell Script – Result ....................................................................................................................2
  • 2. How To Check File and Delete – PowerShell 1 | P a g e Overview In this post / snippet we will demonstrate, deleting a file on the system if the file exists. 1) If the file exists it will be deleted 2) If the file doesn’t exist a message box will be displayed 3) If the user doesn’t key any file, an error message will be displayed. Applies To Tested on Windows 10, Windows 2008 R2, Windows 2012. Pre-Requisites To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or “Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”. Policy Type Purpose Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode. AllSigned Only scripts signed by a trusted publisher can be run. RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run. Unrestricted No restrictions; all Windows PowerShell scripts can be run. Current Execution Policy To know the current run the PowerShell cmdlet; Get-ExecutionPolicy To list execution policies that can be configured run the PowerShell cmdlet; Get-ExecutionPolicy -List
  • 3. How To Check File and Delete – PowerShell 2 | P a g e PowerShell Script – Delete File This script read filename from user input, check if file exists and delete it. PowerShell Snippet Clear-Host # # Read filename # $ReadFileName=Read-Host -Prompt "Enter file to be deleted" if ($ReadFileName -eq "") { Write-Host "No Filename Issued..." [console]::beep(900,300) [System.Windows.MessageBox]::Show($ReadFileName + ' - No Filename Issued ', "Delete File Status") return } if (Test-Path $ReadFileName) { del $ReadFileName } else { Write-Host $ReadFileName does not exists! [console]::beep(900,300) [System.Windows.MessageBox]::Show($ReadFileName + ' - File does not exists.', "Delete File Status") } PowerShell Script – Result When the script is executed, if the file exists file will be deleted or an error message will be shown.