Friday, May 11, 2018

How To Check and Delete a File


Overview

In this post / snippet we will demonstrate, deleting a file on the system if the file exists.
  • If the file exists it will be deleted.
  • If the file doesn’t exist a message box will be displayed.
  • 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 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



PowerShell Script Info – Delete File

This script read filename from user input, check if file exists and delete it.

PowerShell Snippet


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 Stop
         Write-Host "`nRequested - $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 
     }
 }


PowerShell Output – Check and Delete File

When script is executed; below output will be displayed, with different messages.

Message – when requested file is “Deleted”.




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”.



SlideShare Information

A step by step guide with screenshot is uploaded to slideshare.



No comments:

Post a Comment