Skip to main content

Mission Possible with Windows PowerShell

In Windows 8.1 the number of tasks you can perform using Windows PowerShell has increased. So, let’s take a look at some of the new, improved, or just plain fun functions you can perform using Windows PowerShell.

Mission 1: Replace Diskpart.exe

In Windows 8, it was possible to use Windows PowerShell as a replacement for DISM.exe, but capturing and applying was missing. This has now been fixed and we can now use the Expand-WindowsImage cmdlet and the Add-WindowsImage cmdlets.

#To capture a disk as a wim:
Add-WindowsImage -ImagePath c:\imagestore\custom.wim -CapturePath d:\ -Name "The D Drive"

#To apply a Wimfile to a disk
Expand-WindowsImage -ImagePath c:\imagestore\custom.wim -Index 1 -ApplyPath e:\

Mission 2: Export and Import the Start screen

Sometimes it is necessary to modify the Start screen. The way to do this in previous versions of Windows was not that easy and that has changed with Windows 8.1. We can now use Export-StartLayout and Import-StartLayout. In this example, I’m importing the Start screen layout directly into the running operating sytem, but it could also be imported into a mounted image, just point –MountPath to the correct drive.

#Export the StartLayout
New-Item -Path c:\StartScreen -Type Directory -ErrorAction SilentlyContinue
Export-StartLayout -Path c:\StartScreen\StartScreen.BIN -As BIN

#Import the StartLayout
Import-StartLayout -LayoutPath C:\StartScreen\StartScreen.BIN -MountPath C:\

Mission 3: Download and Deploy Windows PowerShell Help

The help in Windows PowerShell is a bit better if you update it, but instead of updating every machine directly from the Internet, you can download the help files to a folder and then import them. This way, it is unnecessary for every person to download their own copy.

#Save Help in C:\Help
New-Item -Path C:\Help -ItemType Directory
Save-Help -DestinationPath c:\help –Verbose

#Update Help from C:\Help
Update-Help -SourcePath C:\help

Mission 4: Put That Configuration on That PC

One if the coolest new features is Desired State Configuration, which basically means that we can define how a Windows computer should be configured. We can use Desired State Configuration to install an application or a server fully configured as a web server, the possibilities are more or less unlimited. In this example, I need to install RDCMan on a Windows 8.1 computer, but the problem is that I really need to have .NET Framework 3 installed, since that is required for RDCman to be installed and working.

The script is divided into three parts; first, we define what we need and what part is depending on what; second, we generate the configuration including a .MOF file; and third, we execute the Desired State against the target using the Configuration cmdlet.

Configuration StandardConfig {
   Node "$env:COMPUTERNAME"
   {
     #Settings for Remote Desktop Connection Manager
     Package RDCMan
     {
      Ensure = "Present"
      Path  = "$Env:SystemDrive\Install\RDCMan.msi"
      Name = "RDCMan"
      ProductId = "173A2B7F-535A-4403-A454-B41531EF0D7F"
      DependsOn = "[Script]NetFX3"
     }
    #Settings for .Net Framework
    Script NetFX3
     {
     SetScript = 
      { 
       Enable-WindowsOptionalFeature -FeatureName NetFx3 -Online -NoRestart
      }
     TestScript = 
      {
      $NetFx3State = Get-WindowsOptionalFeature -FeatureName NetFx3 -Online
      If($NetFx3State.state -Like "Enabled"){Return $True}else{Return $False}
      }
     GetScript = 
      {
      $NetFx3State = Get-WindowsOptionalFeature -FeatureName NetFx3 -Online
      }          
     }
   }
} 


#Execute the Configuration Function
StandardConfig

Start-DscConfiguration -Wait -Verbose -Path .\StandardConfig

Mission 5: Test Connectivity Not Using Ping.exe

I have been using ping like everyone else, but with Windows PowerShell you can switch over to something a bit more interesting. Using the Test-Connection cmdlet, you can now very easily use the information that gets returned back as a verification. Let’s assume that I would like to test the connectivity to a Microsoft web server. Using ping behind a normal firewall, this would not work. Using Test-Connection; however, will work since I can define how it should test it, in this case, using HTTP:

Test-NetConnection www.microsoft.com -InformationLevel Detailed -CommonTCPPort HTTP -Verbose

And the reply back will be:

Mission 6: We Need Kiosk Mode

Kiosk Mode is a new feature in Windows 8.1 and of course there are some nice Windows PowerShell commands available to utilize it. We can assign applications to the user, as well as clear and get the assigned applications. First, we run need to find out what applications the user has currently. This means we need to log on with that account once to get all the application provisioned correctly; this can be done in a task sequence if needed. Then, we assign the application to the user and verify it. Suddenly, it is fun to build the kiosk computer using an automated deployment process to the lobby. :-)

#Get the AppPackage for the user
Get-AppxPackage -User KioskUser | select Name

#Assign an Application to that user
Set-AssignedAccess -AppName Microsoft.BingSports -UserName KioskUser

#Verify result
Get-AssignedAccess

The result after running the command:

There are many other missions you can achieve; these are just a sample. I recommend that you download the latest version of Windows PowerShell (Windows PowerShell 4.0) today and see Scripting with Windows PowerShell for more details.

Top Tasks

About the Author

Mikael Nystrom photoMikael Nystrom is a Microsoft MVP and Microsoft Certified Trainer (MCT) specializing in deployment, virtualization, and management. He has been involved in Technology Adoption Programs (TAPs) for several Microsoft products and technologies including Windows Server, Hyper-V, Windows 7, and Windows 8. In addition to his work as a speaker, trainer, and consultant, Mikael frequently shares technical news and insights through his blog and on Twitter.

Related Resources

Stay Informed