PowerShell Script to request and delete print jobs older than "x" days

I started combining this PowerShell Script, I hope that we will replace some of the tasks that are currently performed manually

I use

get-Date.AddDays()

function

I use ISE to build the Script, and when testing, I get the output if I select the "starttime" property, but this seems like a trick because all the values ​​are zero, d would like to use the "timeubmitted" property, but the date seems to be output in odd, which, in my opinion, is not read correctly, because my requests with "timeubmitted" always go blank.

it is displayed in this format if you are performing an open request

20120416030836.778000-420

that's what i still have.

ignore | 'format-table' that i just see if i get the desired result

#Clears Old Print Jobs on Specified server

#Sets Execution Policy for Script to run
Set-ExecutionPolicy RemoteSigned -Force

#establishes variable for cutoff date
$d = Get-Date
$old = $d.AddDays(-4)

#Queries WMI and retrieves print jobs
Get-WmiObject -class win32_printjob -namespace "root\CIMV2" | where-object {$_.timesubmitted -lt
"$old"} | ft caption,document,jobid,jobstatus,owner,timesubmitted
+3
1

PowerShell WMI ScriptMethod, WMI .NET:

Get-WmiObject Win32_PrintJob | 
Where-Object { $_.ConvertToDateTime($_.TimeSubmitted) -lt $old } |
Foreach-Object { $_.Delete() }
+7

All Articles