Powershell cmdlets one-by-one or How to replace diskpart with powershell

Yes, yes I know… The Swedish Powershell MVP Niklas Goude will probably kick my ass if he ever found out I wrote this blog post…
 
But not all of us were born composing powershell scripts with our eyes closed, so here are some baby steps (one-by-one cmdlets) to help you get started with understanding how awesome powershell is 🙂
 
What I did was that I sat down and got-help in powershell and actually figured out how to create a virtual machine and a virtual disk a.k.a I’m trying to step away from diskpart (that we all know and love)
 
Creating a virtual machine, mounting an ISO and starting it, FTW
 
First of all, you need Powershell 3.0 to accomplish this, that either means you need a Windows Server 2012 or Windows 8 host with Hyper-V installed. (Yes Hyper-V is included in both the client and the server)
 
Create, start a virtual machine and mount an ISO with Powershell
 
Run Powershell as administrator and execute the following:
New-VM -Name VM01 -MemoryStartupBytes 1024MB
The command creates a new virtual machine named VM01 with 1024 MB memory.
 
Now you need to create a virtual disk for the machine:
 
New-VHD -Path C:\VM\VM01.vhdx -SizeBytes 30GB -Dynamic
After creating the disk you’d want to attach it to the virtual machine
Add-VMHardDiskDrive -VMName VM01 -Path C:\VM\VM01.vhdx
Then to mount an ISO you execute
Set-VMDvdDrive -VMName VM01 -ControllerNumber 1 -Path C:\ISO\windows.ISO
And then start the machine!
 
Start-vm –name VM01
 
 
Create, select, partition and format VHD in Powershell or…
“How to replace diskpart with powershell”
 
So diskpart, create vdisk and so on, how-to do the same thing in powershell…
 
Create a virtual disk
 
New-VHD -Path C:\VM\windows.vhdx -SizeBytes 30GB -Dynamic
 
Mount the VHD (instead of attaching it in diskpart)
 
Mount-VHD -Path C:\VM\windows.vhdx
 
Find out which disk number the mounted VHD got
 
Get-Disk
 
Bring the disk online (and YES you specify 1 instead of 0 if you want to take it offline)
 
Set-Disk -Number 2 -IsOffline 0
 
Make the disk writable (and YES you specify it to be Read only with 1)
 
Set-Disk -Number 2 -isReadOnly 0
 
Initialize the disk
 
Initialize-Disk -Number 2 -PartitionStyle MBR
 
Create a partition on disk 2 and automatically assign a driveletter
 
New-Partition -Disknumber 2 -UseMaximumSize -AssignDriveLetter
 
Format the volume
 
Get-Partition -Disknumber 2 | Format-Volume -FileSystem FAT32
By the way, if you try to eject the virtual disk in windows explorer or disk management you will most likely get an error message, so if you want to

Dismount the VHD execute the following
Dismount-VHD -Path C:\vm\windows.vhdx

And then there are the rest of the Storage Cmdelts awesomeness

User data migration with USMT – where what how

I don’t like long explanations, directory locations or long talks on the phone. I enjoy long walks in the forest or long concerts.
So if you want to use USMT version (while I am writing this) it’s version 6.2.9200.16384 *version 5.0) there is a pretty Technet blog post that explain more about USMT 5.0 vs 4.0 etc… but wait, it says USMT 5.0 in the blog post title, but the post refers to USMT 4.0
Then there’s always the User State Migration Tool (USMT) Technical Reference or 113000 hits on google containing the words USMT and Video.

So you don’t really need this just-another-blog-post do you 🙂
I just want to tell you TWO THINGS about USMT that I wanted to know:
1. Where the hell is the commandline USMT located
2. How do I extract a .mig file (usmt.mig)

Well, the command line version of USMT is installed in the following folder when you download and install Microsoft ADK

USMT location after installing ADK (Assessment and Deployment Kit)

…and I don’t won’t to call usmtutils.exe from
C:\Program Files (x86)\Windows Kits\8.0\Assessment and deployment kit\User State Migration Tool\amd64… *phew* every time SO I copied the necessary files from C:\Program Files\Bla\Bla\Bla\User State Bla\amd64 to C:\USMT instead.
After doing that there will be a usmtutils.log file in C:\USMT when you run the command usmtutils.exe /? and a short explanation is displayed in the command prompt.

Needed dlls when copying usmtutils.exe to other locations

So now I can extract an usmt.mig file with usmtutils.exe by executing the following

msutils.exe /extract c:\usmt\mig\USMT.MIG C:\usmt\extract

Need to know shortcuts in Windows 8 and Windows Server 2012

Ok, so you don’t want to scroll around forever looking for shortcut keys in Windows 8 and Windows Server 2012 – these are my top three:

1. Windows key + i to find the Power options

Windows key + i

2. Windows key + X to find the administrator shortcuts

Windows key + x

3. Right click CMD to find options such as Run as administrator
OR
simply press CTRL + SHIFT and left click CMD to send Run as administrator to cmd,
OR with CMD selected just click CTRL + SHIFT + ENTER
Works with task bar pinned legacy applications as well….

Run, run, run as admin

….So, those are my Three favorites amongst many.

Windows Server 2012 and Internet Explorer 10

Feeling stressed lately, giving up, downloading Chrome on Windows Server 2012 to download tools in your Windows Server 2012 lab environment?
I know how you feel, it’s a bit frustrating when you try to download files in Internet Explorer, you add the page to trusted websites, lower the security levels for the internet zone, turn off protected mode and still you are presented by The big white

Caution: When downloading files from Internet Explorer you need to ask yourself – what am I downloading and Why, am I only installing Chrome because I want to download Acrobat Reader, why am I installing Acrobat Reader on A SERVER?

The Solution is Knowledge!

If Internet Explorer Enchanced Security is Enabled – File downloads from the Internet Zone is turned off.

But for Lab/Testing/Iwillnotusethisinproduction you can turn off Internet Explorer Enchanded Security:
Open Server Manager, go to Dashboard, click Configure this local server, and in the properties section next to IE Enhanced Security Configuration click the ON and select OFF.
If you want to read more about enchanced security features in Windows Server 2012 and IE visit this webpage and read it.
BUT if you follow the instructions on the mentioned page and open IEs default page to find out more about Internet Explorer Enhanced Security Configuration by clicking the link, you will most likely be presented with “This page can’t be displayed”.
Solution: Note the url res://ieframe.dll/IESecHelp.htm – if you change it to iesetup instead of ieframe.dll…etc you will access the correct help page.

How to make users ask for permission to access folders the Windows 8/Server 2012 way

-“I can’t access this folder”
-“What’s the name of the folder you are trying to access”?
-“I don’t know… G something”
-“…Ok… can you tell me the name of  a colleague who’s got access to the folder”?
-“Yes! There’s Peter, but I don’t know his last name”
And thus the interrogation continues…

But wait! With Windows 8 and Server 2012 we configure Access Denied Assistance Message and let the user click a button to provide us with the information we need when managing access to folders.
How neat isn’t this:

The configured message on the Windows 8 Client
What the mail function looks like on the client

 So basically, the user can request permission, you have configured a folder owner e-mail adress, what information the mail will contain (such as folder path etc) for a shared folder, and voila, no communication needed from either side 🙂

Where to configure Request Assistance message for a share

The customized message specified for a share

And of course you can configure this with Group Policy to apply if for all files and folders.
Computer configuration\Policies\Administrative Templates\System\Access-Denied Assistance 


Anyway, there are a LOT of more (and I dare say it) awesome features that makes it easier for both the user and the administrator when it comes to file and resource management, but that’s another day and another blog post. 🙂

Adding or removing features with Powershell (or Dism)

Installing or uninstalling, waiting for updates, re-starting, staring at that “please-waaaaiiiit-wheel” can make you feel like… you’re wasting your time.
What if you want to install a feature with a script, or manually/graphically adding one doesn’t work – you start over, you restart the machine, go back to add/remove features and realize that – well – it didn’t install the rest of the management features… *sigh* instead of waiting… clicking Next… Filling out forms… Restarting… and so on bla bla.

So! We’ll look at different ways to add, install or uninstall features in Windows 8 and Windows Server 2012:

Uninstalling or Installing Windows Features using Poweshell
Run Powershell as Administrator and execute the following:
Get-WindowsFeature
to view a list of Available and Installed Features

If you want to remove a Windows feature such as the Graphical user interface on Windows Server 2012 (GUI) and run core you can uninstall Graphical Management Tools and Infrastructure (Server-Gui-Mgmt-Infra) and Server Graphical Shelll (Server-Gui-Shell) you can run the following command to uninstall the windows features
Uninstall-WindowsFeature Server-Gui-Mgmt-Infra,Server-Gui-Shell -Restart

So if you want to Install a windows feature you simply run Install instead:
Install-WindowsFeature Server-Gui-Mgmt-Infra,Server-Gui-Shell -Restart

Uninstalling or Installing Windows Features using DISM
Run CMD as administrator and execute the following
dism /online /get-features | more
To install a feature using dism, for example .Net 3.5, .Net2.0 for older applications use
dism /online /enable-feature /featurename:NetFx3

Getting errors, or how to install features that are missing installation files using an ISO
Did you install a Core server and can’t get the windows feature for GUI to install? Grab a Windows Server 2012 (or Windows 8 ISO if you’re trying to install .net on a client) and use install.wim to get a hold of the c:\windows\winsxs folder which has all the nessecary files to install a windows feature.

With Powershell
1. Mount the ISO and create a folder called C:\mount. Mount a Server 2012 or Windows 8 ISO depending on your working environment and note the drive letter, in this example I will use D:\, by running the following:
Mount-Diskimage C:\Lab\windows8orserver2012.iso
2. Determine what Index number you will use (Datacenter or Enterprise etc) by running the following command:
Get-WindowsImage -ImagePath D:\sources\install.wim
3. Mount the install.wim from the ISO by running the following commandline:
Mount-WindowsImage -ImagePath D:\sources\install.wim -Index 4 -Path C:\mount -Optimize -ReadOnly
4. Install the feature using the winsxs folder from your mounted install.wim image by running the following commandline:
Install-WindowsFeature Server-Gui-Mgmt-Infra,Server-Gui-Shell Restart
Source c:\mountwim\windows\winsxs
5. To dismount the wimfile run
Dismount-WindowsImage -Path C:\mount -Discard Dismount-WindowsImage -Path C:\mount -Discard

If you need to create a folder you can use New-Item cmdlet to create a folder:
New-Item c:\ Mount -type directory


With Dism
1. Mount the ISO and note the drive letter and create a folder called C:\Mount, in this example I will use D:\
2. Determine what Index number you will use (Datacenter or Enterprise etc) by running the following command:
Dism /get-wiminfo /wimfile:D:sources\install.wim
3. Mount the install.wim file (in this example the Index number is 4) by running the following commandline:
Dism /mount-wim /WimFile:D:\sources\install.wim /Index:4 /MountDir:c:\mount /readonly
4. Install the windows feature by running the following
dism /online /enable-feature /featurename:servercore-fullserver /featurename:server-gui-shell /featurename:server-gui-mgmt /all /source:N:\sources\sxs

If you need to create a folder use mkdir.

Get help with Powershell -examples


Sitting at Mikael Nyströms Windows Server 2012 lab I yet again realize that it’s never too late to get help… when it comes to powershell 🙂
I’m not a Powershell Guru (…yet,but to be Jedi I want) and until that day emerges I need to Get-Help, so Powershell help to the rescue!

Searching for help with Powershell ISE
Powershell ISE is useful for building, debugging, testing scripts and copy-pasting instead of using Powershell and Notepad. Other than that I find it useful when getting help.
Run Powershell ISE and open Commands by either running Show-Command or click Show Command Window.


Show Command Window

Type in New-virtual and select the New-VirtualDisk cmdlet, click the field next to FriendlyName* and click F1 to bring up Help and notice that you can use the Search field to find cmdlets etc.

Getting help and detailed examples
Type Get-Help get- and notice the snippet window with examples that you can run, type get-v and use TAB to complete the command Get-Help Get-VirtualDisk and hit Enter.

Snippets


If you get promted to update help select Yes and use the Up Arrow key to re-select the latest command Get-Help Get-VirtualDisk.
At the end of the page under Remarks note that you can view examples, detailed, full and -Online.
If you type Get-Help Get-VirtualDisk -Online (Unfortunately some cmdlets are not yet present in the help file)

And there are a lot of other ways to get help, if you haven’t visited The Scripting Guys blog -do, or if you’re Swedish, you can download the free e-book “Kom igång med Powershell” from Niklas Goudes site and my favorite page (one of many) is a list of Powershell cmdlets at Technet