Sysadmins shortcuts

Sysadmins shortcuts

Dynamic Powershell Menu build on current files in directory

PowershellPosted by Thomas Petersen Mon, February 18, 2013 16:38:59
The main goal here is to automate patching of .wim files for Windows client/server installation, to save the initial updating after installing.

When updating your image, the installation will be running quite a lot faster, than if you would have to make the install, and then start patching - no rocket science here...

Usually there would be a few different images, so the menu will be based on the current .wim files located in the image directory.

Basically the files are read into an array, and an option for choosing all images and one for abotring is added.

So the menu part of the script looks like the following:


#location of .wim files. Being read into the $wimfiles array

$wimfiles=Get-ChildItem c:\scripts *.wim

function ToArray

# typecasting single object to array




$output = @();




$output += $_;




return ,$output;



#building menu


Write-output "Please choose image(s) to update"

do {

write-host $i $wimfiles[$i].name


} while ($i -lt ($wimfiles.count))

write-host $i 'All images'

write-host ($i+1) 'Abort'

#read choice, and validate against number of files+2 (#files, All files, Abort)

do {$WimID = Read-host "Choice"}

while ((0..($wimfiles.count+1)) -notcontains $WimID)

if ($WimID -eq ($wimfiles.count+1)) {

write-output "Aborting..."



elseif ($WimID -eq ($wimfiles.count)) {

# All wimfiles chosen, read files into $WimPatch array

$WimPatch = Get-ChildItem c:\scripts *.wim


Else {

# Single file chosen, read into $WimPatch and call function ToArray, to typecast the single file to an array

$WimPatch = Get-ChildItem c:\scripts $WimFiles[$WimID].Name | ToArray


write-host *******************

write-host "Image(s) to be updated:"



do {

write-host $WimPatch[$i].Name


} while ($i -lt ($WimPatch.count))

#now we have chosen which file(s) to work on, and they are ready to be sent to the patch function

More to come later...

  • Comments(0)

Fill in only if you are not real

The following XHTML tags are allowed: <b>, <br/>, <em>, <i>, <strong>, <u>. CSS styles and Javascript are not permitted.