1. Windows PowerShell Basics – Remove Item
i | P a g e
Table of Contents
Overview.......................................................................................................................................................1
Applies To..................................................................................................................................................1
Pre-Requisites ...........................................................................................................................................1
Current Execution Policy...........................................................................................................................1
PowerShell Script – Check and Delete File....................................................................................................2
Code Snippet – Check and Delete File ......................................................................................................2
PowerShell Output – Check and Delete File .........................................................................................3
2. Windows PowerShell Basics – Remove Item
1 | P a g e
Overview
In this guide we will demonstrate as to how to delete a file on a server after checking if the file exists with
Windows PowerShell script.
Applies To
Tested on Windows 10, Windows 2008 R2 and Windows 2012.
Pre-Requisites
Launch PowerShell Command Console or PowerShell ISE.
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”.
Each Policy type and its purpose is shown in the below table.
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. Windows PowerShell Basics – Remove Item
2 | P a g e
PowerShell Script – Check and Delete File
In this PowerShell script we will have few validations and subsequently delete the file in the folder,
validations as follows.
Directory / path exists
File Exists
This script will exit if directory / file doesn’t exists that the user provides for deletion. Also, ensure the file
is NOT in use by any process or application.
Code Snippet – Check and Delete File
The code snippet is for demonstrating “File Deletion”.
Clear-Host
#
# Read Folder
#
$ReadDirectory=Read-Host -Prompt "Enter Directory"
if ( -NOT (Test-Path $ReadDirectory)) {
Write-Host "`nInvalid Directory" -ForegroundColor Green -BackgroundColor Black
[console]::Beep(900,300)
exit 1
} else {
$ReadFileName=Read-Host -Prompt "Enter filename to be deleted"
if ($ReadFileName -eq "") {
Write-Host "`nNo Filename Issued..." -ForegroundColor Green -BackgroundColor Black
[console]::Beep(900,300)
[System.Windows.MessageBox]::Show($ReadDirectory + '' + $ReadFileName + ' No Filename Issued ', "Delete
File Status")
return
}
if (Test-Path $ReadDirectory$ReadFileName) {
Set-Location $ReadDirectory
Push-Location $ReadDirectory
Remove-Item $ReadFileName -ErrorAction Inquire
Write-Host "`n Requested - $ReadDirectory$ReadFileName has been deleted" -ForegroundColor Green -
BackgroundColor Black
# Optional, validating by listing filename
# gci $ReadDirectory$ReadFileName -ErrorAction Ignore
} else {
Write-Host "`nFilename $ReadFileName does not exists!" -ForegroundColor Green -BackgroundColor Black
[console]::Beep(900,300)
[System.Windows.MessageBox]::Show($ReadDirectory + '' + $ReadFileName + ' - File does not exists ',
"Delete File Status")
return
}
}
4. Windows PowerShell Basics – Remove Item
3 | P a g e
PowerShell Output – Check and Delete File
When script is executed; below output will be displayed, with different messages.
Message – when requested file is “Deleted”.
5. Windows PowerShell Basics – Remove Item
4 | P a g e
Message – when the “Directory Invalid / Incorrect Directory”.
Message – when requested file is “File Not Found”.
Message – when requested file is “Still in Use by application / process”.