
PS Scripting – USB install media: Add or Remove Windows Features
This sample PowerShell script will show how to use menus, and how to make selections using an Out-GridView table. In addition, we will mount a WIM image for offline servicing, and save changes made to it. Please notice that this script is quite simple, intended as an example to show how to get started with mildly interactive PS scripting.
To get started, please download the example script from my OneDrive: AddRemove.ps1
The line numbers used in this post refer to those line numbers shown in OneDrive Preview.. When reading the script in OneDrive, all remark lines (lines beginning with #) are shown in green. When the script is executed, these lines are ignored. I recommend that you read all remarks to learn what’s happening next.
OK, let’s go. Lines 1 to 23, are all remark lines that provide basic information about the script. Before script itself starts, we auto-elevate the script in lines 25 to 60. This makes it possible to run the script from the Explorer context menu simply by selecting Run with PowerShell. Thus, the need not launch an elevated PowerShell.
Next, we will create two variables. First, the variable $EmptySpace, which as its name suggests creates an empty space of 9 lines is created in lines 62 to 78 :

When we later mount a WIM image, a PS progress bar occupies 5 lines at the top of the window, starting from line 3. Using the $EmptySpace variable, we can push other script output below that progressbar, instead of having it hidden behind it.
In lines 80 to 97, we create a simple menu, and store it in the variable $AddRemove:
In lines 99 to 138, we ask the user to designate the drive letter for the USB source drive, and check that it contains a valid install.wim file:
Next, in lines 140 to 154 we check for available Windows editions in a given WIM file, and write them out as a table:
In lines 156 to 172 we mount the selected edition for offline servicing in a temporary folder, utilizing the $EmptySpace variable to write script output below the progressbar:
The variable $EmptySpace is called in line 162 to keep the output message in line 168 visible:
The main function of this script happens in lines 174 to 214. In a Do – While loop, we call the menu (variable $AddRemove), asking if user wants to add or remove optional features. Depending on the user’s choice(s) a list of either currently disabled or enabled Windows optional features on mounted image is shown as a Grid View table. The user can select required features by holding down the CTRL key, then finally click OK to add or remove whatever features were selected:
Once the user selects menu option 3 – QUIT from the menu, all changes made are saved to the Windows image, and the script exits:
That’s it! The USB install media is updated, with selected Windows optional features either added or removed as per the user’s instructions. It’s a helpful tool, then, for managing Windows install images. I hope you enjoy it.
Kari
Author: Kari Finn
A former Windows Insider MVP, Kari started in computing in the mid 80’s writing code for VAX / VMS systems. Since then, he’s worked in a variety of IT positions. He specializes in Windows image capture, customization, repair and deployment as well as Hyper-V virtualization. Kari is a proud Team Member at number #1 Windows site TenForums.com.