{"id":8564,"date":"2021-09-17T09:21:04","date_gmt":"2021-09-17T13:21:04","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=8564"},"modified":"2021-09-17T09:21:08","modified_gmt":"2021-09-17T13:21:08","slug":"cleaning-up-powershell-jobs","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/","title":{"rendered":"Cleaning Up PowerShell Jobs"},"content":{"rendered":"\n<div class=\"wp-block-image is-style-default\"><figure class=\"alignleft size-full\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/pexels-cottonbro-4108715.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"107\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/pexels-cottonbro-4108715.jpg\" alt=\"https:\/\/www.pexels.com\/photo\/person-wearing-white-pants-and-white-socks-standing-beside-brown-broom-4108715\/\" class=\"wp-image-8565\"\/><\/a><\/figure><\/div>\n\n\n\n<p>I am a heavy user of PowerShell jobs. Not only background jobs but also scheduled jobs.  They are a critical element in my daily workflow. Every time a job runs, especially scheduled jobs, a job artifact remains which you can see using Get-Job.  For scheduled jobs, I try to keep this to a minimum by specifying a MaxResultCount with Register-ScheduledJob. I rarely need to check the results of the job but I like being able to see the job result. However, all of this still leads to a large number of jobs. Here's a taste of what this looks like on my daily driver.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/psjobs.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"566\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/psjobs-1024x566.png\" alt=\"\" class=\"wp-image-8566\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/psjobs-1024x566.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/psjobs-300x166.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/psjobs-768x424.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/psjobs-850x470.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/psjobs.png 1053w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>On one hand, this really isn't that big a deal. The jobs aren't consuming much in the way of disk space or memory and it is nice to have the information if I need to troubleshoot something. Still, it might be nice to be able to clean this up. I could easily wipe away all the jobs with a one-line command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\"> Get-Job | where state -ne running | Remove-Job<\/code><\/pre>\n\n\n\n<p>But, I'd like to leave at least 1 or 2 of the newest job results behind so I can monitor everything. For this task, I wrote a PowerShell function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">#requires -version 5.1\n\n#remove all but the newest X number of jobs based on the job name.\n\nFunction Remove-OldJob {\n    [cmdletbinding(SupportsShouldProcess)]\n    [OutputType(\"None\", \"Job\")]\n    Param(\n        [Parameter(Position = 0, HelpMessage = \"Specify the newest number of jobs to save for each job name. The max is 10.\")]\n        [ValidateRange(1, 10)]\n        [int]$Newest = 1,\n        [Parameter(HelpMessage = \"Display the revised job list\")]\n        [switch]$Passthru\n    )\n    Write-Verbose \"Starting $($myinvocation.MyCommand)\"\n    Write-Verbose \"Saving the newest $newest job result(s)\"\n    Write-Verbose \"Group all jobs that aren't running by their name\"\n    $jobs = Get-Job | Where-Object state -NE running | Group-Object -Property Name\n    if ($jobs) {\n        foreach ($item in $jobs) {\n            if ($item.count -gt $Newest) {\n                Write-Verbose \"Processing $($item.count) total jobs for $($item.name)\"\n                $item.group | Sort-Object psendtime -Descending | Select-Object -Skip $Newest | Remove-Job\n            } #foreach item\n            else {\n                Write-Verbose \"Skipping job $($item.name)\"\n            }\n        }\n    } #if jobs\n    else {\n        Write-Warning \"No jobs found.\"\n    }\n\n    if ($Passthru) {\n        Get-Job | Sort-Object -Property Name\n    }\n    Write-Verbose \"Ending $($myinvocation.MyCommand)\"\n}<\/code><\/pre>\n\n\n\n<p>The function works by grouping job results by name.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/grouping-jobs.png\"><img loading=\"lazy\" decoding=\"async\" width=\"769\" height=\"311\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/grouping-jobs.png\" alt=\"\" class=\"wp-image-8567\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/grouping-jobs.png 769w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/grouping-jobs-300x121.png 300w\" sizes=\"auto, (max-width: 769px) 100vw, 769px\" \/><\/a><\/figure>\n\n\n\n<p>I wrote the function to take a parameter indicating how many of the newest jobs I want to keep. For each group, if the count is equal to or above this value, I sort the group of jobs by their end time, skipping the specified number of jobs and remove them.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$jobs = Get-Job | Where-Object state -NE running | Group-Object -Property Name\nif ($jobs) {\n    foreach ($item in $jobs) {\n        if ($item.count -gt $Newest) {\n            Write-Verbose \"Processing $($item.count) total jobs for $($item.name)\"\n            $item.group | Sort-Object psendtime -Descending | Select-Object -Skip $Newest | Remove-Job\n        } #foreach item\n        else {\n            Write-Verbose \"Skipping job $($item.name)\"\n        }\n    }\n} #if jobs<\/code><\/pre>\n\n\n\n<p>Because I'm changing the state of my system, I added SupportsShouldProcess to the cmdletbinding attribute. Because Remove-Job supports -WhatIf, I don't have to code anything special. When I run my command with -WhatIf, the preference gets passed to Remove-Job.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-whatif.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"603\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-whatif-1024x603.png\" alt=\"\" class=\"wp-image-8569\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-whatif-1024x603.png 1024w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-whatif-300x177.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-whatif-768x452.png 768w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-whatif-850x500.png 850w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-whatif.png 1108w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<p>I can then run the command and clean up the job list.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-result.png\"><img loading=\"lazy\" decoding=\"async\" width=\"659\" height=\"491\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-result.png\" alt=\"\" class=\"wp-image-8570\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-result.png 659w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/remove-job-result-300x224.png 300w\" sizes=\"auto, (max-width: 659px) 100vw, 659px\" \/><\/a><\/figure>\n\n\n\n<p>The -Passthru parameter gets the remaining jobs and displays them sorted by name. Normally, I wouldn't include the sort in the function, but in this case, the sorted results make it easy to verify the results. Which in this case is no more than 2 job results for each named job.<\/p>\n\n\n\n<p>You may not need to clean up old job results, but hopefully, there's something in my code that you can use in your own work. As always, comments and questions welcome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am a heavy user of PowerShell jobs. Not only background jobs but also scheduled jobs. They are a critical element in my daily workflow. Every time a job runs, especially scheduled jobs, a job artifact remains which you can see using Get-Job. For scheduled jobs, I try to keep this to a minimum by&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New on the blog: Cleaning Up #PowerShell Jobs","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[465,224,534,383,540],"class_list":["post-8564","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-background-job","tag-function","tag-powershell","tag-scheduled-job","tag-scripting"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Cleaning Up PowerShell Jobs &#8226; The Lonely Administrator<\/title>\n<meta name=\"description\" content=\"I use PowerShell jobs a lot. Here&#039;s a PowerShell function I wrote to help me keep all of the job results organized.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Cleaning Up PowerShell Jobs &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"I use PowerShell jobs a lot. Here&#039;s a PowerShell function I wrote to help me keep all of the job results organized.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-17T13:21:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-17T13:21:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/pexels-cottonbro-4108715.jpg\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Cleaning Up PowerShell Jobs\",\"datePublished\":\"2021-09-17T13:21:04+00:00\",\"dateModified\":\"2021-09-17T13:21:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/\"},\"wordCount\":396,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/09\\\/pexels-cottonbro-4108715.jpg\",\"keywords\":[\"Background Job\",\"Function\",\"PowerShell\",\"scheduled job\",\"Scripting\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/\",\"name\":\"Cleaning Up PowerShell Jobs &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/09\\\/pexels-cottonbro-4108715.jpg\",\"datePublished\":\"2021-09-17T13:21:04+00:00\",\"dateModified\":\"2021-09-17T13:21:08+00:00\",\"description\":\"I use PowerShell jobs a lot. Here's a PowerShell function I wrote to help me keep all of the job results organized.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/09\\\/pexels-cottonbro-4108715.jpg\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/09\\\/pexels-cottonbro-4108715.jpg\",\"width\":150,\"height\":107},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8564\\\/cleaning-up-powershell-jobs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Cleaning Up PowerShell Jobs\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Cleaning Up PowerShell Jobs &#8226; The Lonely Administrator","description":"I use PowerShell jobs a lot. Here's a PowerShell function I wrote to help me keep all of the job results organized.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/","og_locale":"en_US","og_type":"article","og_title":"Cleaning Up PowerShell Jobs &#8226; The Lonely Administrator","og_description":"I use PowerShell jobs a lot. Here's a PowerShell function I wrote to help me keep all of the job results organized.","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/","og_site_name":"The Lonely Administrator","article_published_time":"2021-09-17T13:21:04+00:00","article_modified_time":"2021-09-17T13:21:08+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/pexels-cottonbro-4108715.jpg","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Cleaning Up PowerShell Jobs","datePublished":"2021-09-17T13:21:04+00:00","dateModified":"2021-09-17T13:21:08+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/"},"wordCount":396,"commentCount":1,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/pexels-cottonbro-4108715.jpg","keywords":["Background Job","Function","PowerShell","scheduled job","Scripting"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/","name":"Cleaning Up PowerShell Jobs &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/pexels-cottonbro-4108715.jpg","datePublished":"2021-09-17T13:21:04+00:00","dateModified":"2021-09-17T13:21:08+00:00","description":"I use PowerShell jobs a lot. Here's a PowerShell function I wrote to help me keep all of the job results organized.","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/pexels-cottonbro-4108715.jpg","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/09\/pexels-cottonbro-4108715.jpg","width":150,"height":107},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8564\/cleaning-up-powershell-jobs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Cleaning Up PowerShell Jobs"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":2297,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/2297\/sql-saturday-129-session-material\/","url_meta":{"origin":8564,"position":0},"title":"SQL Saturday 129 Session Material","author":"Jeffery Hicks","date":"May 14, 2012","format":false,"excerpt":"I spoke this past weekend at a SQL Saturday event in Rochester, NY. My first SQL Saturday event and it was a lot of fun. A great turnout and some very interested attendees. I did three PowerShell sessions on jobs, scheduled jobs\/tasks and an intro to workflows. The latter talk\u2026","rel":"","context":"In &quot;Conferences&quot;","block_context":{"text":"Conferences","link":"https:\/\/jdhitsolutions.com\/blog\/category\/conferences\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2011\/10\/talkbubble-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3449,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/3449\/a-better-powershell-get-scheduled-job-results\/","url_meta":{"origin":8564,"position":1},"title":"A Better PowerShell Get Scheduled Job Results","author":"Jeffery Hicks","date":"September 17, 2013","format":false,"excerpt":"Yesterday I posted a quick update on my code to get the most recent scheduled job result in PowerShell. I had been using a simple script. But the more I thought about it, the more I realized I really did need to turn it into a function with more flexibility.\u2026","rel":"","context":"In &quot;Best Practices&quot;","block_context":{"text":"Best Practices","link":"https:\/\/jdhitsolutions.com\/blog\/category\/best-practices\/"},"img":{"alt_text":"get-scheduledjob-latest-ogv","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv-1024x327.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv-1024x327.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest-ogv-1024x327.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2833,"url":"https:\/\/jdhitsolutions.com\/blog\/scripting\/2833\/get-scheduled-job-results\/","url_meta":{"origin":8564,"position":2},"title":"Get Scheduled Job Results","author":"Jeffery Hicks","date":"March 5, 2013","format":false,"excerpt":"One of my favorite features in PowerShell 3.0 is the ability to run a PowerShell job as a scheduled task. I can easily setup a PowerShell background job to run a script but have it registered as a scheduled task. All you need is PowerShell 3.0. The job results are\u2026","rel":"","context":"In &quot;Powershell 3.0&quot;","block_context":{"text":"Powershell 3.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},"img":{"alt_text":"talkbubble-v3","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2012\/05\/talkbubble-v3-150x150.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3648,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/3648\/remove-all-but-most-recent-powershell-job\/","url_meta":{"origin":8564,"position":3},"title":"Remove All but Most Recent PowerShell job","author":"Jeffery Hicks","date":"April 21, 2014","format":false,"excerpt":"I like sending little PowerShell tweet tips, but this one is a bit too long for Twitter. Here is a one-liner to remove all but the most recent job result for each job name. get-job | group Name | foreach { $_.group | sort PSEndTime -descending | Select -skip 1\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3445,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell-3-0\/3445\/get-most-recent-powershell-scheduled-job-result\/","url_meta":{"origin":8564,"position":4},"title":"Get Most Recent PowerShell Scheduled Job Result","author":"Jeffery Hicks","date":"September 16, 2013","format":false,"excerpt":"I think I've posted this before, but in any event even if I did, I've tweaked this code a bit. When you create a scheduled job in PowerShell 3.0, by default PowerShell will keep results on disk for the last 32 times the job ran. This can make it a\u2026","rel":"","context":"In &quot;Powershell 3.0&quot;","block_context":{"text":"Powershell 3.0","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell-3-0\/"},"img":{"alt_text":"get-scheduledjob-latest2","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest2-1024x233.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest2-1024x233.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2013\/09\/get-scheduledjob-latest2-1024x233.png?resize=525%2C300 1.5x"},"classes":[]},{"id":917,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/917\/understanding-powershell-background-jobs\/","url_meta":{"origin":8564,"position":5},"title":"Understanding PowerShell Background Jobs","author":"Jeffery Hicks","date":"September 9, 2010","format":false,"excerpt":"Last night I spoke to the CNY .NET Developers Group about background jobs in Windows PowerShell. Even though the audience was primarily developers, I discussed jobs from an administrator's perspective, that is, using cmdlets. The job feature in PowerShell 2.0 is pretty amazing and you don't need any programming skills.\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8564"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8564\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}