{"id":7051,"date":"2015-03-26T00:01:00","date_gmt":"2015-03-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/03\/26\/use-powershell-to-add-files-to-offline-windows-image\/"},"modified":"2019-02-18T10:30:08","modified_gmt":"2019-02-18T17:30:08","slug":"use-powershell-to-add-files-to-offline-windows-image","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-add-files-to-offline-windows-image\/","title":{"rendered":"Use PowerShell to Add Files to Offline Windows Image"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to add files to an offline Windows image.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I have a number of VHDs that I need to add files to. I know that I can use File Manager to mount the VHD and then copy the files, but I am hoping that I can use Windows PowerShell to do this. The VHDs are for virtual machines that I run in my lab. Can you help?<\/p>\n<p>&mdash;AM<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello AM,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I am a bit sore. I finally made it back into the gym yesterday, and my trainer seemed to have it in for me. Anyway, I am listening to Alan Parsons on my Zune, sipping a nice cup of Earl Grey tea, and going through my email to <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>. I am thinking that later tonight I might make it back to the gym to work out the soreness.<\/p>\n<p>Speaking of sore&#8230;<\/p>\n<p>AM, just reading about all that mousing around makes my wrist hurt. The good thing is that Windows PowerShell can indeed mount and service offline Windows images. (This functionality was introduced in Windows 8 and Windows Server 2012.)<\/p>\n<p>One of the things I like to do is to put things back where I found them&mdash;this includes my current working directory. So, I love to use Push and Pop. <b>Push-Location<\/b> stores the current working location. I use it before I set my location using <b>Set-Location<\/b>. I do not need to use a variable and <b>Get-Location<\/b> because Windows PowerShell already knows about a &ldquo;location stack.&rdquo; All I need to do is call <b>Push-Location<\/b>, and Windows PowerShell will automatically remember where I start from. Here is the code:<\/p>\n<p style=\"margin-left:30px\">Push-Location<\/p>\n<p style=\"margin-left:30px\">Set-Location c:\\<\/p>\n<p>Because I want things to occur in a specific order, I create a simple workflow&mdash;and I do mean simple. It is no more complicated than a very basic Windows PowerShell function. I use the <b>Workflow<\/b> keyword, specify a name, and create some variables. This is shown here:<\/p>\n<p style=\"margin-left:30px\">WorkFlow Service-Image<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;$image = &quot;E:\\vms\\vhd\\c1\\c1.vhdx&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;$path = &quot;c:\\image&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;$scripts = &#039;C:\\PoshScripts&#039;<\/p>\n<p>Now I create the folder that will hold my mounted image, and I use the <b>Sequence<\/b> keyword to specify that I want commands to appear in a certain order:<\/p>\n<p style=\"margin-left:30px\">MD $path<\/p>\n<p style=\"margin-left:30px\">&nbsp; Sequence {<\/p>\n<p>I want to first mount my Windows image, then I want to copy some Windows PowerShell scripts to a folder, then I want to dismount my Windows image and remove the folder that I created. All this is shown here:<\/p>\n<p style=\"margin-left:30px\">Mount-WindowsImage -ImagePath $image -Path $path -Index 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Copy-Item -Path $scripts -Destination &quot;C:\\image\\poshScripts&quot; -Recurse<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Dismount-WindowsImage -Path $path -Save<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; RD $path -Force<\/p>\n<p style=\"margin-left:30px\">&nbsp;} }<\/p>\n<p>The last things I do is call my workflow and pop back to my original working directory. This is shown here:<\/p>\n<p style=\"margin-left:30px\">Service-Image<\/p>\n<p style=\"margin-left:30px\">Pop-Location<\/p>\n<p>Here is the complete script:<\/p>\n<p style=\"margin-left:30px\">Push-Location<\/p>\n<p style=\"margin-left:30px\">Set-Location c:\\<\/p>\n<p style=\"margin-left:30px\">WorkFlow Service-Image<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;$image = &quot;E:\\vms\\vhd\\c1\\c1.vhdx&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;$path = &quot;c:\\image&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;$scripts = &#039;C:\\PoshScripts&#039;<\/p>\n<p style=\"margin-left:30px\">&nbsp;MD $path<\/p>\n<p style=\"margin-left:30px\">&nbsp; Sequence {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Mount-WindowsImage -ImagePath $image -Path $path -Index 1<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Copy-Item -Path $scripts -Destination &quot;C:\\image\\poshScripts&quot; -Recurse<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Dismount-WindowsImage -Path $path -Save<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; RD $path -Force<\/p>\n<p style=\"margin-left:30px\">&nbsp;} }<\/p>\n<p style=\"margin-left:30px\">Service-Image<\/p>\n<p style=\"margin-left:30px\">Pop-Location<\/p>\n<p>As shown in the following image, when I run the script, I see a few things in the output:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-26-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-26-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One thing I see is a path to the DISM log. So I go there and open the log in Notepad. The log is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-26-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-26-15-02.png\" alt=\"Image of files\" title=\"Image of files\" \/><\/a><\/p>\n<p>One thing to note is that the DISM log appends to the end. So I need to go all the way to the end of the log to see what happened during my Windows PowerShell script. I see that it said everything was successful. Cool. I decide to mount the image via the Windows Files Explorer so that I can check to ensure that my Windows PowerShell scripts were indeed copied. As shown in the following image, it was a success:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-26-15-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-3-26-15-03.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>AM, that is all there is to using Windows PowerShell to add files to a Windows image. Join me tomorrow when I will talk about more cool Windows PowerShell 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><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to add files to an offline Windows image. &nbsp;Hey, Scripting Guy! I have a number of VHDs that I need to add files to. I know that I can use File Manager to mount the VHD and then copy the files, but I am [&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":[576,271,3,4,45,382],"class_list":["post-7051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dism","tag-hyper-v","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-workflow"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to add files to an offline Windows image. &nbsp;Hey, Scripting Guy! I have a number of VHDs that I need to add files to. I know that I can use File Manager to mount the VHD and then copy the files, but I am [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7051","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=7051"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7051\/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=7051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}