<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Media And Microcode : Remove-DVRBySeries</title><link>http://blogs.msdn.com/mediaandmicrocode/archive/tags/Remove-DVRBySeries/default.aspx</link><description>Tags: Remove-DVRBySeries</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Microcode: Cleaning up DVR with Get-RecordedTV</title><link>http://blogs.msdn.com/mediaandmicrocode/archive/2008/07/21/microcode-cleaning-up-dvr-with-get-recordedtv.aspx</link><pubDate>Mon, 21 Jul 2008 03:51:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8759886</guid><dc:creator>JamesBrundage</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/mediaandmicrocode/comments/8759886.aspx</comments><wfw:commentRss>http://blogs.msdn.com/mediaandmicrocode/commentrss.aspx?PostID=8759886</wfw:commentRss><description>&lt;p&gt;The first simple way we can approach cleaning up DVR is by making the assertion that shows you don't want as much you won't record as often.&amp;#160; Suppose you record a movie once, or a game, or the pilot of a really bad TV show.&amp;#160;&amp;#160; After a month or so it's still on your hard drive, taking up space but for some reason avoiding the Windows Media Center's cleanup bots.&lt;/p&gt;  &lt;p&gt;Now that we've got a &lt;a href="http://blogs.msdn.com/mediaandmicrocode/archive/2008/07/21/microcode-cleaning-up-get-recordedtv-with-select-object.aspx"&gt;fairly mature Get-RecordedTV&lt;/a&gt;, we can use it to delete a certain number of files from your collection until we reach a desired amount of freed space.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;function Remove-DVRBySeries($cleanupSize = 1gb, $episodeCount = 1)     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; $shows = @()      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; # Pipe the results of Get-RecordedTV so that we can see progress      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; $perc = 0 &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; Get-RecordedTV | Foreach-Object {     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $perc+=5      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if ($perc -gt 100) { $perc = 0 }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Write-Progress &amp;quot;Getting DVR Metadata&amp;quot; &amp;quot;$($_.Series) - $($_.Episode) - $($_.Channel)&amp;quot; -perc $perc      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $shows+=$_      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; $showsbySeries = $shows | Group-Object Series      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; $allSeries = $showsBySeries |      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Where-Object { $_.Count -le $episodeCount} |      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Sort-Object Count      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; foreach ($series in $allSeries)      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; foreach ($e in $series.Group) {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $response =Read-Host &amp;quot;Delete $($e.Series) - $($e.Episode) (Yes/No)&amp;quot;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if ($response -ilike &amp;quot;*Yes*&amp;quot;) {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if ($e.File) {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Write-Progress &amp;quot;Deleting $($e.Series) - $($e.Episode)&amp;quot; `      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;$($e.Size / 1mb) megabytes saved - $(($cleanupSize - $e.Size)/1mb) remaining&amp;quot;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Remove-Item $e.File      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $cleanupSize-=$e.Size      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Write-Error &amp;quot;$($e.Series) - $($e.Episode) File Not Found&amp;quot;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else {      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Write-Progress &amp;quot;Skipping show by user request&amp;quot; &amp;quot;$($e.Series) - $($e.Episode)&amp;quot;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if ($cleanupSize -le 0) { return }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;}&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This script will remove shows with one or fewer episodes until it has saved 1gb of space.&amp;#160; If you want to make it save more, use the cleanupSize parameter.&amp;#160; If you want it to check series with more episodes, use the -episodeCount parameter. It will ask to make sure you want to delete each show, and will stop once it has saved you enough space.&lt;/p&gt;  &lt;p&gt;The script above uses a lot of simple PowerShell benefits.&amp;#160; Let's take a closer look at each:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;CleanupSize uses 1gb as its default.&amp;#160; In PowerShell, you can use gb,mb,and kb as numbers, e.g. 1gb, 10mb, or 640kb&lt;/li&gt;    &lt;li&gt;By piping Get-RecordedTV to ForEach-Object, I am able to store the results of Get-RecordedTV as they come in, instead of waiting for all of them to be completed, I can use Write-Progress to display each show as it comes in.&amp;#160; Since I don't know how long it will take, I can just wrap the progress bar around whenever I get to 100%.&lt;/li&gt;    &lt;li&gt;Group-Object will group all of the shows by the value of the series property&lt;/li&gt;    &lt;li&gt;Sort-Object is used to sort the results of Group-Object by the number of items in each group (in our case, the number of recorded episodes of a series)&lt;/li&gt;    &lt;li&gt;Write-Progress is used again to update the user as files are deleted or skipped&lt;/li&gt;    &lt;li&gt;Read-Host displays a prompt and asks for user input&lt;/li&gt;    &lt;li&gt;-ilike is a case insentive like, so $response -ilike will match &amp;quot;Yes&amp;quot;, &amp;quot;yeS', or &amp;quot;Yes Sir&amp;quot;, sadly, the logic will also catch &amp;quot;Yes, I mean No!&amp;quot;&lt;/li&gt;    &lt;li&gt;Write-Error politely informs me that a file cannot be deleted, but does not stop execution of the script (this the&amp;#160; important difference between Write-Error and throw)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;That's it.&amp;#160; We've brought one train of thought to an end.&amp;#160; With this script, I was able to quickly and easily cleanup almost 40gb of shows that I wasn't watching.&amp;#160; I hope it helps you clean up your hard drive, and I hope that explaining how to get here has increased your understanding of PowerShell scripting and problem solving.&lt;/p&gt;  &lt;p&gt;Hope this helps,&lt;/p&gt;  &lt;p&gt;James&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8759886" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/PowerShell/default.aspx">PowerShell</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Microcode/default.aspx">Microcode</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/DVR/default.aspx">DVR</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Get-RecordedTV/default.aspx">Get-RecordedTV</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Write-Error/default.aspx">Write-Error</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Sort-Object/default.aspx">Sort-Object</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Write-Progress/default.aspx">Write-Progress</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Group-Object/default.aspx">Group-Object</category><category domain="http://blogs.msdn.com/mediaandmicrocode/archive/tags/Remove-DVRBySeries/default.aspx">Remove-DVRBySeries</category></item></channel></rss>