Ask Learn
Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
After writing my script to allow a non-administrative user to control Hyper-V – I started thinking about how it would be nice if I could easily add and remove users from being Hyper-V administrators – without having to run a script each time. Which lead to this:
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
{
# We are running "as Administrator" - so change the title and background color to indicate this
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
$Host.UI.RawUI.BackgroundColor = "DarkBlue"
clear-host
}
else
{
# We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition;
# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
# Exit from the current, unelevated, process
exit
}
# Create "Hyper-V Administrators" group
$LocalComputer = [ADSI] "WinNT://$env:computername"
$HvAdminGroup = $LocalComputer.create("Group", "Hyper-V Administrators")
$HvAdminGroup.setinfo()
# Get the SID for the newly created group
$HvAdminGroupSID = (gwmi Win32_Group | ?{$_.Name -eq "Hyper-V Administrators"}).sid
# Add current user to Hyper-V Administrators group
$fixedUserName = $myWindowsID.Name -replace "\\","/"
$HvAdminGroup.add("WinNT://$env:computername/$fixedUserName")
# Get the current AzMan store location from the registry
$AzManStoreLocation = (Get-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization").StoreLocation
# Open the AzMan store
$AzManStore = new-object -ComObject "AzRoles.AzAuthorizationStore"
$AzManStore.Initialize(2, $AzManStoreLocation)
# Handle the default Hyper-V AzMan store and the SCVMM AzMan store
if (@($AzManStore.Applications | ? {$_.Name -contains "Hyper-V services"}).count -eq 1)
{
$HyperVAzManStore = $AzManStore.OpenApplication("Hyper-V services")
}
elseif (@($AzManStore.Applications | ? {$_.Name -contains "Virtual Machine Manager"}).count -eq 1)
{
$HyperVAzManStore = $AzManStore.OpenApplication("Virtual Machine Manager")
}
else
{
Write-Host "Unable to find AzMan application group."
Write-Host -NoNewLine "Press any key to continue..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit
}
# Get the administrator role from the Hyper-V service in the AzMan store
$HyperVAdministratorsRole = $HyperVAzManStore.OpenRoleAssignment("Administrator")
# Add the Hyper-V Admin group to the AzMan store
$HyperVAdministratorsRole.AddMember($HvAdminGroupSID)
$HyperVAdministratorsRole.Submit()
What this script does is to create a local user group – called “Hyper-V Administrators” – and then configures that group to have full access to Hyper-V (it also adds the current user as a member of the “Hyper-V Administrators” group). After running this script you can make other users Hyper-V Administrators by just adding them to the group (with no need to run the script again). Note that the same caveats apply to this script as did to yesterdays script:
Cheers,
Ben
Anonymous
October 06, 2010
Yikes! I do not know how the sample code got that badly mangled. It should be fixed now. Either way - if you grap the .ZIP file attached to the post it should have the correct code.
Cheers,
Ben
Anonymous
October 07, 2014
It runs itself in administrator mode and then it closes itself unexpectedly.
Anonymous
January 11, 2016
I'm running Windows 10 Pro. I"m trying to properly create the "Hyper-V Administrators" group with full access to Hyper-V and include me in that group. I"m doing widows phone development, running an emulator in Hyper-V
Error from "./AzManGroup.ps1"
Value does not fall within the expected range.
At C:usersjeffaDevelopmentAzManGroup.ps1:52 char:1
+ CategoryInfo : OperationStopped: (:) [], ArgumentException + FullyQualifiedErrorId : System.ArgumentException Unable to find AzMan application group. Press any key to continue...
Anonymous
January 14, 2016
Same problem as jhalbrecht. Please advise.
Anonymous
January 19, 2016
Hi, I've encountered the same problem as jhalbrecht and jacob. I need to delegate Hyper-V management to power users and can't accomplish it on Windows 10 Enterprise LTSB 2015. There is no Hyper-V Administrators group, nor it can be created running this script. Well technically it does create the group, but it doesn't do anything. Since there is no capability in Authorization Manager either, how on earth do I delegate the Hyper-V management rights, please? :-D
Anonymous
January 26, 2016
Hi All,
Two part answer to the people with problems on Windows 10:
As of Windows 8 and later - you do not need to do any of what is discussed in this post. It all magically works (which is discussed here: blogs.msdn.com/.../allowing-non-administrators-to-control-hyper-v-updated.aspx)
Except, when it does not work :-( - We have tracked down an issue that can cause the group to be missing when you upgrade from one Windows edition to another. We are working on a fix right now, unfortunately there are no work arounds at this point in time.
Please sign in to use this experience.
Sign in