# sdelete old files with powershell

**URL:** https://forums.powershell.org/t/sdelete-old-files-with-powershell/11649
**Category:** PowerShell Help
**Created:** [November 9, 2018, 12:03pm UTC](https://forums.powershell.org/t/sdelete-old-files-with-powershell/11649 "2018-11-09T12:03:15Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![nmuzic](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@nmuzic](https://forums.powershell.org/u/nmuzic)
#### Post date: [November 9, 2018, 12:03pm UTC](https://forums.powershell.org/t/sdelete-old-files-with-powershell/11649/1 "2018-11-09T12:03:15Z")

</div>

Hi,

I’m trying to find old files in several directories and want them to delete securely with sdelete utility.  
Also, I want to log that sdelete activity and I noticed on the file on which I do not have permission I got an error:

```
SDelete is set for 3 passes.

E:\DIR1\TEST\This_is_test_file.exe...
Error opening E:\DIR1\TEST\This_is_test_file.exe for delete:
Access is denied.

No files/folders found that match E:\DIR1\TEST\This_is_test_file.exe.
```

But, in the sdelete.log I get an output:

```
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
Beginning run at: 11/09/2018 14:58:55
Using shred command: C:\ADMIN\sdelete64.exe
Shred iterations: -p 3
----------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
Shredding file: E:\DIR1\TEST\This_is_test_file.exe
C:\ADMIN\sdelete64.exe -p 3 E:\DIR1\TEST\This_is_test_file.exe
Shred exited: True
Shred return code: 0
Ending at: 11/09/2018 14:58:58
----------------------------------------------------------------------------------------------
```

Can someone help me with capturing those files that actually not deleted and save it to another log file?

This is what I got so far:

```
$Now=Get-Date
$Days="30"
$Lastwrite = $Now.AddDays(-$Days)
$dt = (get-date).ToString("ddMMyyyy")
$CSVFile = 'E:\Log\ToDelete_' + $dt + '.csv'
$SdelogFile = 'E:\Log\ShredLog_' + $dt +'.log'
$sdelete = "C:\ADMIN\sdelete64.exe"
$iterations = "-p 3"
$dirs = 'E:\DIR1','E:\DIR2'

Foreach ($dir in $dirs) {  
Get-ChildItem $dir -File -Recurse | Where-Object {$_.LastwriteTime -le “$Lastwrite”} | Select-Object Name, `  
@{ n = ‘FilePath’; e = { Convert-Path $_.PSPath } },`  
@{ n = ‘LastWriteTime’ ; e = { $\_.LastWriteTime } } |  
Export-Csv -Path $CSVFile -Append -Encoding UTF8 -NoTypeInformation  
}

$Files = Import-Csv -Path $CSVFile | Select-Object -ExpandProperty FilePath | Where-Object { $\_.PSObject.Properties.Value -ne $null}  
Add-content $SdelogFile -value “----------------------------------------------------------------------------------------------------”  
Add-content $SdelogFile -value “----------------------------------------------------------------------------------------------------”  
Add-content $SdelogFile -value “Beginning run at: $Now”  
Add-content $SdelogFile -value “Using sdelete command: $sdelete”  
Add-content $SdelogFile -value “Sdelete iterations: $iterations”  
Add-content $SdelogFile -value “----------------------------------------------------------------------------------------------------”  
Add-content $SdelogFile -value “----------------------------------------------------------------------------------------------------”  
ForEach ($File in $Files){  
Add-content $SdelogFile -value “Shredding file: $file”  
Add-content $SdelogFile -value “$sdelete $iterations $file”  
$proc = Start-Process -FilePath $sdelete -ArgumentList “$iterations `”$file`"" -Wait -NoNewWindow -PassThru  
Add-Content $SdelogFile -value “Sdelete exited: $($proc.HasExited)”  
Add-content $SdelogFile -value “Sdelete return code: $($proc.ExitCode)”  
}  
$end = Get-date  
Add-Content $SdelogFile -value “Ending at: $end”  
Add-content $SdelogFile -value “----------------------------------------------------------------------------------------------------”
```

---

<div class="post-metadata">

### Author: ![fredrik-kacsmarck](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@fredrik-kacsmarck](https://forums.powershell.org/u/fredrik-kacsmarck)
#### Post date: [November 9, 2018, 1:10pm UTC](https://forums.powershell.org/t/sdelete-old-files-with-powershell/11649/2 "2018-11-09T13:10:07Z")

</div>

The problem is that Start-Process doesn’t generate the console output and/or Sdelete don’t generate an error exit code (they seem to be ‘0’ always).

What you could do is not use start-process.

E.g.

```
$result = &$sdelete file_path # note the & to run the command.

if($result -contains 'Access Denied') {
    # Do something else
}
else {
    # Do normal stuff
}
```

---

<div class="post-metadata">

### Author: ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)
#### Post date: [November 9, 2018, 1:17pm UTC](https://forums.powershell.org/t/sdelete-old-files-with-powershell/11649/3 "2018-11-09T13:17:00Z")

</div>

And for logging, you could use transcription using Start-Transcript cmdlet use do write-output or custom logging function print custom messages.

```
Function Log {
    Param(
       [string]$Message,
       [string]$LogfilePath='c:\tempt\customlogfilename.log'
    )
    Add-Content -Path $LogfilePath -Value $Message
}

..
..

Log "First line"
..
Log "Last line"
```

---

<div class="post-metadata">

### Author: ![nmuzic](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@nmuzic](https://forums.powershell.org/u/nmuzic)
#### Post date: [November 11, 2018, 10:02am UTC](https://forums.powershell.org/t/sdelete-old-files-with-powershell/11649/4 "2018-11-11T10:02:10Z")

</div>

Thank you Fredrick, just what I need!

---

<div class="post-metadata">

### Author: ![nmuzic](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@nmuzic](https://forums.powershell.org/u/nmuzic)
#### Post date: [November 12, 2018, 7:16am UTC](https://forums.powershell.org/t/sdelete-old-files-with-powershell/11649/5 "2018-11-12T07:16:41Z")

</div>

Maybe not the right solution,

In your case, I cannot “-p 3” parameter.

---

<div class="post-metadata">

### Author: ![fredrik-kacsmarck](https://avatars.discourse-cdn.com/v4/letter/f/a183cd/32.png) [@fredrik-kacsmarck](https://forums.powershell.org/u/fredrik-kacsmarck)
#### Post date: [November 12, 2018, 9:50am UTC](https://forums.powershell.org/t/sdelete-old-files-with-powershell/11649/6 "2018-11-12T09:50:20Z")

</div>

Yes it may be an issue it works if you do:

```
&$sdelete -p3 c:\tmp\test.txt
```

Seems to be some sort of issue around quote expand the arguments.

What you could do even though it’s not really best practice is to use Invoke-Expression.  
So if you create the command line in a single string and the run Invoke-Expression it also works.

```
$cmdLine = "$sdelete $iterations path_to_file"

Invoke-Expression $cmdLine
```

---

<div class="post-metadata">

### Author: ![nmuzic](https://avatars.discourse-cdn.com/v4/letter/n/5f8ce5/32.png) [@nmuzic](https://forums.powershell.org/u/nmuzic)
#### Post date: [November 13, 2018, 2:01am UTC](https://forums.powershell.org/t/sdelete-old-files-with-powershell/11649/7 "2018-11-13T02:01:21Z")

</div>

Now, with Invoke-Expression, the script works perfectly.

Thank you Fredrik for your help.

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:33pm UTC](https://forums.powershell.org/t/sdelete-old-files-with-powershell/11649/8 "2024-05-16T20:33:42Z")

</div>


