Windows 10: PowerShell Scripting - Create USB Install Media for Windows 10

Discus and support PowerShell Scripting - Create USB Install Media for Windows 10 in Windows 10 Tutorials to solve the problem; How to: PowerShell Scripting - Create USB Install Media for Windows 10 [img] Information The way Windows operating system is delivered and... Discussion in 'Windows 10 Tutorials' started by Brink, Sep 30, 2015.

  1. Brink
    Brink New Member

    PowerShell Scripting - Create USB Install Media for Windows 10


    How to: PowerShell Scripting - Create USB Install Media for Windows 10

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]
    Information The way Windows operating system is delivered and installed has changed quite a lot from days when Windows 3.0 was delivered on 6 floppy disks. To install, you booted PC from floppy 1, which in its turn asked you to eject it and insert floppy 2 to continue, and so on. Fully installed, Windows 3.0 took 5 MB (no, that's not a typo!) space on hard disk.

    Windows 95, although also available as a set of 13 Distribution Media Formatted floppies (DMF to allow each floppy to store more than then standard 1.44MB) was first one to be distributed on CD, Windows XP last one on CD and Vista first one on DVD, size of install media jumping from XP's under 650 MB to Vista's over 3 GB thus forcing the change of media.

    With Windows 10 Microsoft took a big step towards fully electronic distribution; although you can still order Windows 10 on USB flash drive, most of its delivery is done through downloaded ISO images.

    When a user earlier got the physical install media in a box, today the box usually only contains a license, product key and download instructions. To make the install media is left for users themselves. Windows Media Creation Tool (tutorial) can create a USB install media but for instance users downloading Windows Insider ISO images or wanting a custom install media need to create USB install media by themselves.

    This tutorial contains a PowerShell script for creating a bootable USB Windows 10 install media. No additional third party tools is required. Tutorial will continue where PowerShell Scripting - The Basics General Tips Tutorials ended, breaking the script in parts explaining how it was made.

    Please notice that script in tutorial is for creating install media for UEFI based computers with GPT partitioning.



    Contents [/i] Use links below to go to any part, back button of your browser to return to this list.


    [table][tr][td]1: Quick Start[/td] [td]Save the script & run it[/td] [/tr] [tr][td]2: Making a Script - Think ahead[/td] [td]Plan what and in which order will be done[/td] [/tr] [tr][td]3: Making a Script - Start coding[/td] [td]From plan to action[/td] [/tr] [tr][td]4: Making a Script - Remarks[/td] [td]Using remarks in script[/td] [/tr] [tr][td]5: Making a Script - Instructions[/td] [td]Adding on-screen instructions[/td] [/tr] [tr][td]6: Making a Script - Error handling[/td] [td]How to handle possible errors[/td] [/tr] [/table]


    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]
    Note The Quick Start, first part of tutorial is for those who found here searching a quick way to create Windows 10 install USB. If you are not interested in "under the hood" stuff, how the script works and how it's built simply get the script and run it as told in Quick Start and forget the rest of the tutorial.

    The other parts labelled as Making a script are then for those users interested in how to plan and start making scripts. Tutorial will not detail every line in sample script in question, instead it will show how to get started. Going through these parts is not required in any way to be able to use the script for what's it made for, to create USB install media.

    If you get interested in PS scripting, you can find excellent sample scripts and instructions on Microsoft TechNet Script Center and PowerShell pages on docs.microsoft.com:
    - Windows PowerShell Scripting
    - PowerShell Scripting | Microsoft Docs



    1: Quick Start [/i] Save the script & run it 1.1) The script:
    Code:
    1.2) Open an elevated (Run as administrator) PowerShell ISE:
    1. Click Start > W > Windows PowerShell
    2. Right click Windows PowerShell ISE
    3. Select Run as administrator
    1.3 Open the Script pane:

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]


    1.4) Copy the code from 1.1, paste it in script editor. You can resize script editor as you wish (#1 in screenshot), show / hide it (#2). If the Command Add-on at right is taking too much place you can resize it or hide it completely (#3):

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]


    1.5) Save the script (File > Save as) as CreateUSB.ps1. The extension .ps1 tells Windows it is a PowerShell script

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]
    Tip I recommend creating a folder for your PS scripts. In my case I have it on OneDrive in folder %userprofile%\OneDrive\PS Scripts to allow me to share and use scripts on all connecting devices.
    1.6) If you'd rather download the script, download from here:
    - As a PS script file: CreateUSB.ps1
    - As a text file: CreateUSB.txt

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]
    Note Notice if downloading script as PS script (.ps1) file: Running downloaded third party scripts in PowerShell by default requires execution policy being changed from default Restricted mode. See step 2.2 in this tutorial for more information: PowerShell Scripting - The Basics General Tips Tutorials

    If you download script as a text file, open in Notepad, copy all text and paste it in PS ISE script editor as told in 1.4, then save it as told in 1.5, it will be a local script which can be run simply by changing local script policy:

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]


    (Screenshot from step 2.2 in this tutorial: PowerShell Scripting - The Basics General Tips Tutorials )
    1.7) You can now hide script pane (see 1.4) and run the script simply by pressing F5 or selecting Run from File menu. Follow the on-screen instructions to create a bootable Windows 10 install media on USB flash drive:

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]


    1.8) In the future when you want to run the script, open elevated (admin) PS ISE, open the script (File > Open), hide the script pane and press F5

    1.9) To manually run this script on an elevated normal PowerShell or PowerShell ISE, type & sign followed by a space and full path in quotes and hit Enter:

    & "D:\My PS Scripts\CreateUSB.ps1"

    If script is in current PS or PS ISE working folder it can also be started by typing .\<ScriptName.ps1>:

    .\CreateUSB.ps1

    1.10) Notice that when running the script, Windows will detect newly cleaned USB flash drive and will prompt you to format it. Click Cancel to close the prompt, do not in any case click Format!

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]


    When script has formatted USB drive, Windows Explorer will open showing it. You can close Explorer immediately when it happens




    2: Making a script - Think ahead [/i] Plan what and in which order will be done 2.1) Of course you can just start typing your code without any advanced planning, but the longer and more complicated the script will be, the better to spend some time outlining it first

    2.2) In this sample case I wrote myself a short synopsis in Notepad, listing what should be done and in which order. My plan:
    • Opening greetings (explain purpose)
    • Check connected disks, ask user which one is target (USB flash drive)
    • Wipe USB clean, format it
    • Ask source (mounted W10 ISO or folder where ISO contents is copied to)
    • Check if valid W10 install media (simple check > if install.wim / install.esd exists)
    • If not valid > abort
    • If valid > copy ISO or folder contents to USB
    • End > tell user USB install media has been created
    2.3) It looks simple, not worth even telling but I promise you, making a script this long without a plan, just starting to type is much more difficult. It is also easier to stay focused when you have split the script in advance in modules. A good outline helps you stay oriented and can surprisingly often help you to debug it; finding errors in script is easier when you calm down and check your outline




    3: Making a script - Start coding [/i] From plan to action 3.1) Open elevated PS ISE. Some scripts do not require elevation but as this sample one will wipe and format a USB drive, elevation is required. For simple file handling scripts you can use a normal user mode PS.

    3.2) Expand script pane. In this script, I started simply by entering code to clear the screen and show the user general purpose of the script:
    Code:
    3.3) When done, I can check if it works and looks as I wanted by pressing F5:

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]


    3.4) OK, it works. Next step: check all connected disks using Get-Disk cmdlet, show them in formatted table with disk number, name, status, partition system and size in GB. Notice that because I want output to end user remaining as clear as possible, I again clear the screen first with cls and use additional Write-Host cmdlets without any output to create empty lines to make text more readable:
    Code:
    3.5) Again I'll check if it works, pressing F5 runs the script:

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]


    3.6) The next steps, following lines in script include cleaning, wiping the USB flash drive user selects empty. Because this involves risks, an accidentally selected Windows system disk would effectively ruin Windows installation and remove user data, I want to explain to user what's happening and add a warning using a different text colour (magenta looks alarming enough on blue background!).

    Code from above in 3.4 plus explanation and warning added, finally reading user input (USB flash drive number) to variable $USBNUMBER:
    Code:
    3.7) Again, pressing F5 to run script to see if OK:

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]


    (Click to enlarge.)

    3.8) Following my outline step by step I will add all modules until the script is done, doing what I want it to do




    4: Making a script - Remarks [/i] Using remarks in scripts 4.1) PS ignores every line in script starting with a hash (#) sign. It is similar than REM (remark) lines in other coding / scripting languages. To include notes and remarks in script, simply start a line with #, everything after it will be ignored when script is run

    4.2) In my opinion you can't add too many remarks! Even if the script is solely for your personal use, remarks make it easy to see and understand what a long forgotten and found again script does. It also helps to find and extract various parts of scripts as modules to be reused in other scripts

    4.3) Let's look at two examples. The first part of my sample shown in step 3.2 script tells user what it is about. I added the following remark before it (yellow highlight)
    Code:
    4.4) Next remarks were added before the part checking connected disks and before asking user to enter disk number for USB flash drive (highlighted)
    Code:
    4.5) There are no rules regarding use of remarks. Some advanced coders do not note / remark their code at all, some want to add a remark before each line of code. Do as I you prefer, what feels correct. Personally I would not even dream about writing code without remarks




    5: Making a script - Instructions [/i] Adding on-screen instructions 5.1) We have all seen software, batch files and scripts with bad on-screen instructions and illogical output. In my opinion, however much I hate typing, it is essential to be sure that user clearly understands what's done, what will happen and what is expected from user

    5.2) In this sample script, wiping the USB flash drive empty is the one place where everything can go wrong; if user accidentally enters wrong disk number, that disk will be wiped instead of the USB drive. Therefore even using other text color as I did in that part (see steps 3.6 & 3.7) is not overreacting and can be justified

    5.3) Another important part in this script where user needs to know exactly what is expected from user is the step that follows wiping and formatting USB drive, when user needs to tell where Windows installation files are located. In my case, not being a native English speaker, I had to think quite a lot about how to formulate the instructions to make them as clear as possible yet short enough to be sure user will read them:

    Code:
    5.4) I had run the script several times, not trusting that what I saw in script pane is correct, what I want to, but run it to see how the output looks, editing it certainly 15 - 20 times:

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]


    5.5) A script giving not enough information to user increases the risk user does something wrong. Be sure your users understand what they are doing by providing clear instructions




    6: Making a script - Error handling [/i] How to handle possible errors 6.1) In this sample error handling is only used once and in very simple way. When user has entered the drive letter for mounted ISO or path to folder where user has copied ISO files to, script checks if given mounted ISO or folder really contains Windows setup files.

    6.2) This check is done by checking if given ISO or folder contains an install.wim or install.esd file. The idea here is, if WIM or ESD file is not found, the ISO or folder can't contain everything required for Windows installation, therefore script will be aborted:

    Code:
    6.3) An ideal script would check each and every possible error situation and contains "Plan B" for each of them

    That's it geeks! Plan and outline your script, be sure you add enough remarks for you and others to better understand its functions, be sure the on-screen output gives end user enough clear instructions to use your script.

    Kari


    Related Tutorials

    :)
     
    Brink, Sep 30, 2015
    #1
  2. Ztruker Win User

    PowerShell Scripting - Create USB Install Media for Windows 10


    One more change to show only flash drives that are present, not media slots that are empty:

    Code:
    Code:
    Get-Disk | Where-Object –FilterScript {(($_.Bustype -Eq "USB") -and ($_.Size -Ne 0))} | Format-Table Number, Friendlyname, HealthStatus, PartitionStyle,             @{n='Size';e={[int]($_.Size/1GB)}}
    This is how it is now:


    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]


    This is how it would be with the latest code change:


    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]
     
    Ztruker, Oct 26, 2019
    #2
  3. Kari Win User
    PowerShell Scripting - Update Windows 10 USB install media  


    Could you please tell how did you finally manage to mount the WIM file?

    If you already have USB install media for the same version of Windows 10, just delete the install.wim file from its Sources folder, replacing it with your updated one.

    If you don't have USB media done yet, copy the ISO content to a folder for instance ISO_Files, then replace install.wim in Sources with your updated one. Use your preferred method to write those files to a USB flash drive.

    I've done another PS script to do that, you might want to give it a try: PowerShell Scripting - Create USB Install Media for Windows 10 | Windows 10 Tutorials
     
  4. PowerShell Scripting - Create USB Install Media for Windows 10

    Windows 10 USB boot

    Hi there, am not a windows 10 expert. I have a pc in the bedroom on windows 10, I messed up the bootloader trying to fix the xp on that system.

    System wont boot, trying to repair via win 10 usb installer.

    Downloaded the media creation tool to install windows 10 installer on usb stick. Not compatible with a host of usb sticks I have. Tried the usb sticks that others use, but I think they use rufus; as the media creation tool by win 10 does not detect the usb stick.

    So quick question, the win10 machine i am trying to repair the bootloader is a win7 upgraded to win10.
    If i were to use the usb win10 installer to "repair installation" does the version of the iso downloaded via the media creation tool matter?

    There is three options:
    Windows 10
    Windows 10 Home
    Windows 10 N

    Also is there a step by step instead of using rufus as I want to try an alternative method of creating a usb win 10 installer. Preferably with more compatibility.

    System is a asus sabertooth 990 (r2?) with AMD phenom X 6.

    Also is there any other software I can load onto the usb that can repair the bootloader easily?
    That would be the ideal alternative. Thanks
     
    TRUELOVE95, Oct 26, 2019
    #4
  5. Kari Win User
    PowerShell Scripting - Update Windows 10 USB install media


    What / which ISO you have? What Windows 10 version and build, where did you get it?

    Are you entering the source as the folder where ISO contents is copied to? ISO itself cannot be used as source.

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]


    Is the edition list of source image shown correctly?

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]
     
  6. Kari Win User
    PowerShell Scripting - Create USB Install Media for Windows 10


    That's a clever change, to check only USB connected disks filtering with bustype (Ztruker's idea shown as #1 in screenshot)!

    Accordingly, you should then change the output to user; as I mention in tutorial (I admit it's only a subjective personal opinion), it is a sign of good coding to give user as precise and valid information as possible. Because the original output at this point tells about "all your connected disks", I would in your script change that as shown in screenshot (#2):

    PowerShell Scripting - Create USB Install Media for Windows 10 [​IMG]


    Interesting to see how excellent this script will become when we fellow members work with it together PowerShell Scripting - Create USB Install Media for Windows 10 :)
     
Thema:

PowerShell Scripting - Create USB Install Media for Windows 10

Loading...
  1. PowerShell Scripting - Create USB Install Media for Windows 10 - Similar Threads - PowerShell Scripting Create

  2. Media Creation Tool, creating a USB installation medium

    in Windows 10 Installation and Upgrade
    Media Creation Tool, creating a USB installation medium: <p>I find it unbelievable that this tool simply removes any already set up partition(s) from a USB Stick.</p><p>There is not even a warning that any information stored on the stick's partition(s) (even the ones not needed by the tool) will be lost after using the tool.<br...
  3. Powershell Script on Shutdown

    in Windows 10 Performance & Maintenance
    Powershell Script on Shutdown: Hi all, I try to run a Powershell Script (*.ps1) when i shut down my PC. I created the script and it is working fine when i run it. However if i want to run it on a regular Shutdown it seems like it does not start or is not finished, before Windows is killing every running...
  4. Clean install with USB created by Media Creation Tool

    in Windows 10 Installation and Upgrade
    Clean install with USB created by Media Creation Tool: I use the Media Creation Tool to create a bootable USB. When Ive clean installed Ive always waited until the installation has completely finished and Im at the Windows desktop. Only then do I remove the Windows 10 USB flash drive. The USB flash drive stays inserted until...
  5. Create a Windows 10 installation Media Error

    in Windows 10 Installation and Upgrade
    Create a Windows 10 installation Media Error: I'm am trying to fix my computer by installing a Windows 10 installation Media on my empty, more than 9GB USB. Everytime, it loads up to 100%, I get an error. It says 0x80042405 - 0xA001A. Help!...
  6. Trying to create windows 10 installation media

    in Windows 10 Installation and Upgrade
    Trying to create windows 10 installation media: On my work laptop I have the issue "Location not available" after a automatic upgrade and following the instructions to fix this problem I have been trying to create a Windows 10 installation media disk on a second laptop (a Lenovo ideapad 110) but get the error "Unable to...
  7. A Powershell Script for HandBrake

    in Windows 10 Software and Apps
    A Powershell Script for HandBrake: Here's the thing. My system is an age-old i3-530 overclocked to 3.5GHz coupled with GTX 1050 2GB. I have around 1.5TB of tutorial videos that I have to encode due to dearth in HDD space. I want to encode the videos while keeping the folder and subfolder structure intact....
  8. create windows 10 update 1607 usb media

    in Windows 10 Installation and Upgrade
    create windows 10 update 1607 usb media: Hi , I've got an Hp laptop ( energy star , year 2014 ) which prevents booting from Usb stick cause of Secure boot feature when burn the windows 10_1607.iso with Rufus or Microsoft USB/DVD tool . If I used mediacreationtool.exe the created by Microsoft Usb flash drive is...
  9. PowerShell Scripting - Update Windows 10 USB install media

    in Windows 10 Tutorials
    PowerShell Scripting - Update Windows 10 USB install media: How to: PowerShell Scripting - Update Windows 10 USB install media [img] Information A few weeks after release of Windows 10 version 1703 (Creators Update), Microsoft in April 2017 officially announced that Windows in the future will get biannual feature upgrades:...
  10. PowerShell Scripting - The Basics

    in Windows 10 Tutorials
    PowerShell Scripting - The Basics: How to: PowerShell Scripting - The Basics [img] Information A PowerShell script is a collection of commands and cmdlets to be run in logical order, previous lines in script determining values and variables in command lines thereafter. The principle is the same than in...