How to automatically mount a VHD at Startup in Windows 8 (PowerShell)
Introduction
This PowerShell script sample shows how to automatically mount a VHD at Startup in Windows 8.
Scenarios
When users restart the computer, all mounted VHD files need to be mounted again. This script is to solve this problem.
Script
Step 1: Right click PowerShell and select Run as Administrator.
Step 2: Then drag the script to PowerShell console and enter the command shown below.
Note If you do not want to mount the VHD file at start up, you can use run schtasks, find the schedule task MountVHD and delete it.
Here are some code snippets for your reference.
param
(
[Parameter(Mandatory= $True)]
[String]$Identity,
[Switch]$ChangePassowrd
)
Try
{
$Users = Get-ADGroupMember -Identity $Identity
if(!$ChangePassowrd)
{
foreach($user in $Users)
{
$user | Set-ADUser -CannotChangePassword:$true
Write-Progress -Activity "set 'CannotChangePassword'" -Status "$user.Name"
Write-Host "Set user $($user.Name) can not change password successfully." -ForegroundColor Green
}
}
Else
{
foreach($user in $Users)
{
$user | Set-ADUser -CannotChangePassword:$False
Write-Progress -Activity "uncheck 'CannotChangePassword'" -Status "$user.Name"
Write-Host "Set user $($user.Name) can change password successfully." -ForegroundColor Green
}
}
}
Catch
{
Write-Error $_
}
param
(
[Parameter(Mandatory= $True)]
[String]$Identity,
[Switch]$ChangePassowrd
)
Try
{
$Users = Get-ADGroupMember -Identity $Identity
if(!$ChangePassowrd)
{
foreach($user in $Users)
{
$user | Set-ADUser -CannotChangePassword:$true
Write-Progress -Activity "set 'CannotChangePassword'" -Status "$user.Name"
Write-Host "Set user $($user.Name) can not change password successfully." -ForegroundColor Green
}
}
Else
{
foreach($user in $Users)
{
$user | Set-ADUser -CannotChangePassword:$False
Write-Progress -Activity "uncheck 'CannotChangePassword'" -Status "$user.Name"
Write-Host "Set user $($user.Name) can change password successfully." -ForegroundColor Green
}
}
}
Catch
{
Write-Error $_
}
Windows Server 2012
Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.