{"id":2686,"date":"2018-11-02T23:09:54","date_gmt":"2018-11-02T23:09:54","guid":{"rendered":"http:\/\/microsoftgeek.com\/?p=2686"},"modified":"2018-11-02T23:11:57","modified_gmt":"2018-11-02T23:11:57","slug":"how-to-defrag-drives-using-powershell-in-windows-server-2012","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=2686","title":{"rendered":"How to Defrag Drives using Powershell in Windows Server 2012"},"content":{"rendered":"<h2 class=\"user-content_title title -h2 \">Defrag Drives with Optimize-Volume<\/h2>\n<p class=\"user-content_text text \">The new cmdlet we will use is called\u00a0<strong>Optimize-Volume<\/strong>. It is part of the Storage module which utilizes the new CIM infrastructure. You can use this cmdlet to analyze and defrag all types of volumes.<\/p>\n<p class=\"user-content_text text \"><img class=\"_js alignnone size-full wp-image-32901\" title=\"Help optimize volume \" sizes=\"(max-width: 580px) 100vw, 580px\" srcset=\"https:\/\/www.petri.com\/wp-content\/uploads\/sites\/3\/Fig1-Help-Optimize-Volume.png 580w, https:\/\/www.petri.com\/wp-content\/uploads\/sites\/3\/Fig1-Help-Optimize-Volume-300x171.png 300w\" alt=\"Help optimize volume \" width=\"580\" height=\"331\" \/><\/p>\n<p class=\"user-content_text text \">We\u2019ll focus on logical drives like C:.<\/p>\n<h2 class=\"user-content_title title -h2 \">How to Run an Analysis<\/h2>\n<p class=\"user-content_text text \">To run an analysis all you need to do is run a command like this:<\/p>\n<div>\n<div id=\"crayon-5bdcd8ac80f2e605743230\" class=\"crayon-syntax crayon-theme-1c-zapros crayon-font-monaco crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-toolbar\" data-settings=\" show\">\n<div class=\"crayon-tools\">\n<div class=\"crayon-button crayon-nums-button crayon-pressed\" title=\"Toggle Line Numbers\"><\/div>\n<div class=\"crayon-button crayon-wrap-button\" title=\"Toggle Line Wrap\"><\/div>\n<div class=\"crayon-button crayon-popup-button\" title=\"Open Code In New Window\"><\/div>\n<p><span class=\"crayon-language\" style=\"color: #99cc00;\">PowerShell<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"crayon-plain-wrap\"><span style=\"color: #99cc00;\">\u00a0<\/span><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-5bdcd8ac80f2e605743230-1\"><span style=\"color: #99cc00;\">1<\/span><\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-5bdcd8ac80f2e605743230-1\" class=\"crayon-line\"><span style=\"color: #99cc00;\"><span class=\"crayon-r \">PS<\/span> <span class=\"crayon-i\">C<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-o\">&gt;<\/span> <span class=\"crayon-r \">Optimize-Volume<\/span> <span class=\"crayon-i\">G<\/span> \u2013<span class=\"crayon-i\">Analyze<\/span><\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p class=\"user-content_text text \">But for some reason unknown to me, the cmdlet doesn\u2019t write any sort of result to the pipeline! Instead you have to use the<span style=\"color: #99cc00;\"> \u2013Verbose<\/span> parameter.<\/p>\n<div>\n<div id=\"crayon-5bdcd8ac80f34671191013\" class=\"crayon-syntax crayon-theme-1c-zapros crayon-font-monaco crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-toolbar\" data-settings=\" show\">\n<div class=\"crayon-tools\">\n<div class=\"crayon-button crayon-nums-button crayon-pressed\" title=\"Toggle Line Numbers\"><\/div>\n<div class=\"crayon-button crayon-wrap-button\" title=\"Toggle Line Wrap\"><\/div>\n<div class=\"crayon-button crayon-popup-button\" title=\"Open Code In New Window\"><\/div>\n<p><span class=\"crayon-language\" style=\"color: #99cc00;\">PowerShell<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"crayon-plain-wrap\"><span style=\"color: #99cc00;\">\u00a0<\/span><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-5bdcd8ac80f34671191013-1\"><span style=\"color: #99cc00;\">1<\/span><\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-5bdcd8ac80f34671191013-1\" class=\"crayon-line\"><span style=\"color: #99cc00;\"><span class=\"crayon-r \">PS<\/span> <span class=\"crayon-i\">C<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-o\">&gt;<\/span> <span class=\"crayon-r \">Optimize-Volume<\/span> <span class=\"crayon-i\">G<\/span> \u2013<span class=\"crayon-i\">Analyze<\/span> <span class=\"crayon-cn\">-verbose<\/span><\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p class=\"user-content_text text \">You should get something like Figure 2:<\/p>\n<p class=\"user-content_text text \"><img class=\"_js alignnone size-full wp-image-32902\" title=\"Optimize Volume Verbose\" sizes=\"(max-width: 580px) 100vw, 580px\" srcset=\"https:\/\/www.petri.com\/wp-content\/uploads\/sites\/3\/fig2-Optimize-Volume-Verbose.png 580w, https:\/\/www.petri.com\/wp-content\/uploads\/sites\/3\/fig2-Optimize-Volume-Verbose-300x256.png 300w, https:\/\/www.petri.com\/wp-content\/uploads\/sites\/3\/fig2-Optimize-Volume-Verbose-469x400.png 469w\" alt=\"Optimize Volume Verbose\" width=\"580\" height=\"494\" \/><\/p>\n<p class=\"user-content_text text \">So you can get the information you need, but it takes an extra step. If you want to save the results, you\u2019ll need to redirect the verbose output.<\/p>\n<div>\n<div id=\"crayon-5bdcd8ac80f36445777648\" class=\"crayon-syntax crayon-theme-1c-zapros crayon-font-monaco crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-toolbar\" data-settings=\" show\">\n<div class=\"crayon-tools\">\n<div class=\"crayon-button crayon-nums-button crayon-pressed\" title=\"Toggle Line Numbers\"><\/div>\n<div class=\"crayon-button crayon-wrap-button\" title=\"Toggle Line Wrap\"><\/div>\n<div class=\"crayon-button crayon-popup-button\" title=\"Open Code In New Window\"><\/div>\n<p><span class=\"crayon-language\" style=\"color: #99cc00;\">PowerShell<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"crayon-plain-wrap\"><span style=\"color: #99cc00;\">\u00a0<\/span><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-5bdcd8ac80f36445777648-1\"><span style=\"color: #99cc00;\">1<\/span><\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-5bdcd8ac80f36445777648-1\" class=\"crayon-line\"><span style=\"color: #99cc00;\"><span class=\"crayon-r \">PS<\/span> <span class=\"crayon-i\">C<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-o\">&gt;<\/span> <span class=\"crayon-r \">Optimize-Volume<\/span> <span class=\"crayon-i\">G<\/span> \u2013<span class=\"crayon-i\">Analyze<\/span> \u2013<span class=\"crayon-i\">verbose<\/span> 4<span class=\"crayon-o\">&gt;<\/span><span class=\"crayon-i\">c<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-i\">work<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-i\">GDrive<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-i\">txt<\/span><\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p class=\"user-content_text text \">On the positive side, you can specify a remote computer using\u00a0<span style=\"color: #99cc00;\"><strong>\u2013CIMSession<\/strong><\/span>. The remote computer must be running Windows Server 2012 or Windows 8.<\/p>\n<div>\n<div id=\"crayon-5bdcd8ac80f38277625308\" class=\"crayon-syntax crayon-theme-1c-zapros crayon-font-monaco crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-toolbar\" data-settings=\" show\">\n<div class=\"crayon-tools\">\n<div class=\"crayon-button crayon-nums-button crayon-pressed\" title=\"Toggle Line Numbers\"><\/div>\n<div class=\"crayon-button crayon-wrap-button\" title=\"Toggle Line Wrap\"><\/div>\n<div class=\"crayon-button crayon-popup-button\" title=\"Open Code In New Window\"><\/div>\n<p><span class=\"crayon-language\" style=\"color: #99cc00;\">PowerShell<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"crayon-plain-wrap\"><span style=\"color: #99cc00;\">\u00a0<\/span><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-5bdcd8ac80f38277625308-1\"><span style=\"color: #99cc00;\">1<\/span><\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-5bdcd8ac80f38277625308-1\" class=\"crayon-line\"><span style=\"color: #99cc00;\"><span class=\"crayon-r \">PS<\/span> <span class=\"crayon-i\">C<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-o\">&gt;<\/span> <span class=\"crayon-r \">Optimize-Volume<\/span> <span class=\"crayon-i\">C<\/span> \u2013<span class=\"crayon-i\">Analyze<\/span> \u2013<span class=\"crayon-e\">Cimsession <\/span><span class=\"crayon-r \">CHI-DC03<\/span> \u2013<span class=\"crayon-i\">verbose<\/span> 4<span class=\"crayon-o\">&gt;<\/span><span class=\"crayon-i\">c<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-i\">work<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-i\">dc03<\/span><span class=\"crayon-cn\">-c<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-i\">txt<\/span><\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p class=\"user-content_text text \">You can either specify a computer name, as I\u2019ve done here, or an existing CIM session object. This makes it (sort of) easy to analyze the same drive across multiple computers.<\/p>\n<h2 class=\"user-content_title title -h2 \">How to Defrag a Drive<\/h2>\n<p class=\"user-content_text text \">When the time comes to perform the defrag, we can use the same cmdlet, but without the\u00a0<span style=\"color: #99cc00;\"><strong>\u2013Analyze<\/strong><\/span>\u00a0parameter.<\/p>\n<div>\n<div id=\"crayon-5bdcd8ac80f3a634299957\" class=\"crayon-syntax crayon-theme-1c-zapros crayon-font-monaco crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-toolbar\" data-settings=\" show\">\n<div class=\"crayon-tools\">\n<div class=\"crayon-button crayon-nums-button crayon-pressed\" title=\"Toggle Line Numbers\"><\/div>\n<div class=\"crayon-button crayon-wrap-button\" title=\"Toggle Line Wrap\"><\/div>\n<div class=\"crayon-button crayon-popup-button\" title=\"Open Code In New Window\"><\/div>\n<p><span class=\"crayon-language\" style=\"color: #99cc00;\">PowerShell<\/span><\/p>\n<\/div>\n<\/div>\n<div class=\"crayon-plain-wrap\"><span style=\"color: #99cc00;\">\u00a0<\/span><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-5bdcd8ac80f3a634299957-1\"><span style=\"color: #99cc00;\">1<\/span><\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-5bdcd8ac80f3a634299957-1\" class=\"crayon-line\"><span style=\"color: #99cc00;\"><span class=\"crayon-r \">PS<\/span> <span class=\"crayon-i\">C<\/span><span class=\"crayon-o\">:<\/span><span class=\"crayon-sy\">\\<\/span><span class=\"crayon-o\">&gt;<\/span> <span class=\"crayon-r \">Optimize-Volume<\/span> <span class=\"crayon-i\">d<\/span> <span class=\"crayon-cn\">-Verbose<\/span> <span class=\"crayon-cn\">-CimSession<\/span> <span class=\"crayon-i\">novo8<\/span><\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p class=\"user-content_text text \">Again, I used \u2013Verbose so I can see what happened in Figure 3.<\/p>\n<p class=\"user-content_text text \"><img class=\"_js alignnone size-full wp-image-32903\" title=\"Optimize Volume Novo8\" sizes=\"(max-width: 580px) 100vw, 580px\" srcset=\"https:\/\/www.petri.com\/wp-content\/uploads\/sites\/3\/fig3-Optimize-Volume-Novo8.png 580w, https:\/\/www.petri.com\/wp-content\/uploads\/sites\/3\/fig3-Optimize-Volume-Novo8-300x256.png 300w, https:\/\/www.petri.com\/wp-content\/uploads\/sites\/3\/fig3-Optimize-Volume-Novo8-469x400.png 469w, https:\/\/www.petri.com\/wp-content\/uploads\/sites\/3\/fig3-Optimize-Volume-Novo8-470x400.png 470w\" alt=\"Optimize Volume Novo8\" width=\"580\" height=\"494\" \/><\/p>\n<p class=\"user-content_text text \"><span style=\"color: #99cc00;\">Optimize-Volume<\/span> will automatically perform additional operations such as trimming, depending on the drive type. So for an SSD drive the cmdlet should automatically retrim, but you can specify<span style=\"color: #99cc00;\"> \u2013Retrim<\/span> if you want.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Defrag Drives with Optimize-Volume The new cmdlet we will use is called\u00a0Optimize-Volume. It is part of the Storage module which utilizes the new CIM infrastructure. You can use this cmdlet to analyze and defrag all types of volumes. We\u2019ll focus on logical drives like C:. How to Run an Analysis To run an analysis all [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"class_list":["post-2686","post","type-post","status-publish","format-standard","hentry","category-microsoft-windows-server-2012"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2686"}],"version-history":[{"count":2,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2686\/revisions"}],"predecessor-version":[{"id":2692,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2686\/revisions\/2692"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}