{"id":5077,"date":"2012-08-26T00:01:00","date_gmt":"2012-08-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/08\/26\/weekend-scripter-automate-formatting-portable-usb-drives-with-powershell\/"},"modified":"2012-08-26T00:01:00","modified_gmt":"2012-08-26T00:01:00","slug":"weekend-scripter-automate-formatting-portable-usb-drives-with-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-automate-formatting-portable-usb-drives-with-powershell\/","title":{"rendered":"Weekend Scripter: Automate Formatting Portable USB Drives with PowerShell"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to automate formatting and copying data to USB drives.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today we leave the mystery writers conference in Nashville, Tennessee and head to northern Kentucky. I will be teaching a Windows PowerShell workshop to a group of Microsoft premier customers, get to see my good friend Robert from California, speak to the first ever Cincinnati Windows PowerShell Users Group, and have dinner with some old friends. The Scripting Wife will be going with me to the Windows PowerShell Users Group meeting, enchanting local merchants, and be out visiting friends and family.<\/p>\n<p>In preparation for the Windows PowerShell class, I agreed to copy a bunch of files to a bunch of USB drives. I really wanted USB 3.0 drives, but they were not available in the size and quantity that I needed. Oh well, there is always something that provides an excuse for a quick Windows PowerShell script! Yes, Windows PowerShell to the rescue!<\/p>\n<h2>Using PowerShell to format a drive<\/h2>\n<p>Like many of the things in Windows PowerShell&nbsp;3.0 and Windows&nbsp;8, the capabilities exist in previous versions of Windows, and they were doable even in Windows PowerShell&nbsp;1.0. The difference is the ease of accomplishing the task. My Windows PowerShell story today begins with two commands. The first is the <b>Format-Volume<\/b> function (yes, I know the specific verb is a bit pushing the boundaries of use&mdash;I mean <b>Format-List<\/b>, <b>Format-Table<\/b>, <b>Format-Wide<\/b> do not necessary lead to <b>Format-Volume<\/b>, do they? Oh well. From a discoverability standpoint, the name makes sense unless you spend a lot of time searching the Help for <b>Set-Volume<\/b>, <b>New-Volume<\/b>, or even <b>Initialize-Volume<\/b> before you finally give up and use <b>Get-Command &ndash;noun *volume*<\/b>, which is how I found the command. By the way, it is not technically a cmdlet. <b>Get-Command<\/b> tells me it is a function, and it resides in the Storage module.<\/p>\n<p>Anyway, I tried to use the <b>Format-Volume<\/b> function to format one of the portable USB drives, and the first thing that happened was that a prompt asked if I wished to continue. OK, so they implemented <b>ShouldProcess<\/b>. That is probably a good thing. It took a bit of playing around, but finally I discovered that to suppress the prompt (a definite requirement if I have any hopes of automating the process), I feed a value to the <b>confirm<\/b><i> <\/i>parameter.<\/p>\n<p style=\"padding-left: 30px\"><b>Note&nbsp;&nbsp;&nbsp;<\/b>This is not included in the documentation, but it makes sense. The length of the <b>NewFileSystemLabel <\/b>property is limited to 11 characters. You can type more, and the command does not generate an error, but when you run the command, the following warning appears in the output.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Format-Volume -DriveLetter e -NewFileSystemLabel PowerShellROCKS -FileSystem<\/p>\n<p style=\"padding-left: 30px\">exfat -Confirm:$false<\/p>\n<p style=\"padding-left: 30px\">WARNING: The volume label is not valid.<\/p>\n<p style=\"padding-left: 30px\">DriveLetter&nbsp; FileSystemL FileSystem&nbsp; DriveType&nbsp;&nbsp; HealthStatu SizeRemaini&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Size<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; abel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ng<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8212;&#8211;&nbsp; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;-&nbsp; &#8212;&#8212;&#8212;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;-<\/p>\n<p style=\"padding-left: 30px\">E&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exFAT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Removable&nbsp;&nbsp; Healthy&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 28.89 GB&nbsp;&nbsp;&nbsp; 28.89 GB<\/p>\n<p>Even though the warning appears, the command still completes and formats the drive. The label reverts the default label for the type of drive. In this example, the label becomes <i>Removable Disk. <\/i><\/p>\n<p>The command line is shown here.<\/p>\n<p style=\"padding-left: 30px\">Format-Volume -DriveLetter E -NewFileSystemLabel PowerShell -FileSystem exfat -Confirm:$false<\/p>\n<p>I decided that I want to use Robocopy<i> <\/i>to perform the actual copy operation. It is built-in to Windows&nbsp;8, and it is very fast. The command line I use is simple: the source, the destination and an <b>\/S<\/b> switch to copy subdirectories. I use the command line shown here.<\/p>\n<p style=\"padding-left: 30px\">robocopy $source $destination \/S<\/p>\n<h2>Playing system sounds<\/h2>\n<p>The nice thing is that Robocopy informs me the progress of each copy operation. When the process completes, I play a system sound to let me know that I can get the next drive and run the script again. Here is the code that plays a system sound.<\/p>\n<p style=\"padding-left: 30px\">[media.SystemSounds]::(&#8220;Hand&#8221;).play()<\/p>\n<p>I have no idea of what the system sounds sound like, so I wrote a quick bit of code to play the system sounds to provide me with an audition. Here is the code that I used to do that.<\/p>\n<p style=\"padding-left: 30px\">Foreach($sound in ([media.systemsounds] | gm -static -MemberType property))<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$sound.Name<\/p>\n<p style=\"padding-left: 30px\">&nbsp;[media.SystemSounds]::($sound.name).play()<\/p>\n<p style=\"padding-left: 30px\">&nbsp;start-sleep -s 1<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\"><b>FormatDriveCopyFiles.ps1 <\/b><\/p>\n<p style=\"padding-left: 30px\">#requires -version 3.0<\/p>\n<p style=\"padding-left: 30px\">ipmo storage<\/p>\n<p style=\"padding-left: 30px\">$source = &#8220;C:\\data\\PowerShellWOrkshop\\&#8221;<\/p>\n<p style=\"padding-left: 30px\">$destination = &#8220;E:&#8221;<\/p>\n<p style=\"padding-left: 30px\">Format-Volume -DriveLetter E -NewFileSystemLabel PowerShell -FileSystem exfat -Confirm:$false<\/p>\n<p style=\"padding-left: 30px\">robocopy $source $destination \/S<\/p>\n<p style=\"padding-left: 30px\">[media.SystemSounds]::(&#8220;Hand&#8221;).play()<\/p>\n<p>When I run the script, the output from the following image displays in the Windows PowerShell ISE.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1172.WES-8-26-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1172.WES-8-26-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is about it for today. I think there is an outlet mall the Scripting Wife wants to take me to before we check-in to the next hotel. Join me tomorrow for more Windows PowerShell cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to automate formatting and copying data to USB drives. Microsoft Scripting Guy, Ed Wilson, is here. Today we leave the mystery writers conference in Nashville, Tennessee and head to northern Kentucky. I will be teaching a Windows PowerShell workshop to a group of [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[38,362,3,12,61,45],"class_list":["post-5077","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-files","tag-powershell-3","tag-scripting-guy","tag-storage","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell to automate formatting and copying data to USB drives. Microsoft Scripting Guy, Ed Wilson, is here. Today we leave the mystery writers conference in Nashville, Tennessee and head to northern Kentucky. I will be teaching a Windows PowerShell workshop to a group of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5077","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=5077"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5077\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=5077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=5077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=5077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}