# encoding: ascii
# api: powershell
# title: sdelete (wipe file)
# description: imitates sysinternals sdelete tool
# version: 0.1
# type: function
# author: greg zakharov
# license: CC0
# function: Remove-FileAbnormally
# x-poshcode-id: 4800
# x-archived: 2017-04-30T09:58:42
# x-published: 2014-01-16T12:20:00
#
#
Set-Alias sdelete Remove-FileAbnormally
function Remove-FileAbnormally {
<#
.SYNOPSIS
Wipe a file.
.EXAMPLE
PS C:\>sdelete E:\bin\whois.exe
.NOTES
Author: greg zakharov
Mailto: gregzakharov@bk.ru
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateScript({Test-Path $_})]
[String]$FileName,
[Parameter(Position=1)]
[ValidateRange(1, 32)]
[Int32]$PassThru = 3
)
begin {
$FileName = cvpa $FileName
$buf = New-Object "Byte[]" 512
$rng = New-Object Security.Cryptography.RNGCryptoServiceProvider
}
process {
try {
if ([Security.Principal.WindowsIdentity]::GetCurrent().Name -ne `
([IO.FileInfo]$FileName).GetAccessControl().Owner) {
throw (New-Object IO.IOException("File is out of current account."))
}
[IO.File]::SetAttributes($FileName, [IO.FileAttributes]::Normal)
$sectors = [Math]::Ceiling((([IO.FileInfo]$FileName).Length / 512))
$fs = New-Object IO.FileStream($FileName, [IO.FileMode]::Open, [IO.FileAccess]::Write)
for ($i = 0; $i -lt $PassThru; $i++) {
for ($j = 0; $j -lt $sectors; $j++) {
$rng.GetBytes($buf)
$fs.Write($buf, 0, $buf.Length)
}
}
$fs.Close()
$stamp = New-Object DateTime($([Int32](date -u %Y) + 23), 1, 1, 0, 0, 0)
[IO.File]::SetCreationTime($FileName, $stamp)
[IO.File]::SetLastWriteTime($FileName, $stamp)
[IO.File]::SetLastAccessTime($FileName, $stamp)
[IO.File]::Delete($FileName)
}
catch {
$exp = $_.Exception.Message;$exp;""
}
}
end {
if ($exp -eq $null) {
Write-Host File $FileName has been deleted with $PassThru passes. -fo Magenta
""
}
}
}