2

How to add a user group in the "Shut down the system" group policy in Windows Server by CMD or PowerShell

I've read some documentation on Microsoft and other sites. Some of them suggest GPRegistryValue for registry-based policies and other recommended third-party software.

The full path of the key is: "Computer Configuration\Windows Settings\Security Settings\Local Policies\User Rights Assignment"

But in my case I cannot use other packages except CMD or PowerShell (UI not available).

Thanks

2

1 Answer 1

1

Windows provides the secedit.exe tool for this and or custom code, as per the link provided in my comment to you.

Also, did you check the mspowershellgallery.com site for modules that assist with local user security policy?

Find-Module -Name '*sec*pol*'
# Results
<#
Version  Name                    Repository Description                                                                                                
-------  ----                    ---------- -----------                                                                                                
2.10.0.0 SecurityPolicyDsc       PSGallery  This module is a wrapper around secedit.exe which provides the ability to configure user rights assignments
1.3.2    Indented.SecurityPolicy PSGallery  Security management functions and resources                                                                
0.0.12   SecurityPolicy          PSGallery  Module that allows getting, adding and removing User Rights Assignment without using secedit.exe
#>

and

Find-Module -Name '*rights*'
# Results
<#
Version Name                        Repository Description
------- ----                        ---------- -----------
1.0.2   cUserRightsAssignment       PSGallery  The cUserRightsAssignment module contains the cUserRight DSC resource ...
1.0.0   UserRightsAssignment        PSGallery  Analyze the effective User Rights Assignments on a computer and compare results
1.0.1   KMaks.ActiveDirectoryRights PSGallery  This module helps with ActiveDirectory ACL auditing.
#>

Update as per '@Vomit IT - Chunky Mess Style', suggestion.

# Doing this with Secedit and Powershell - something I used in the past

#Get SID from current user
$objUser = New-Object System.Security.Principal.NTAccount("$ENV:userdomain\$ENV:username")
$strSID = $objUser.Translate([System.Security.Principal.SecurityIdentifier])
$MySID = $strSID.Value

#Get list of currently used SIDs
secedit /export /cfg tempexport.inf
$curSIDs = Select-String .\tempexport.inf -Pattern "SeShutdownPrivilege "
$Sids = $curSIDs.line
copy .\LogOnAsAService.inf .\LogOnAsAServiceTemplate.inf
add-content .\LogOnAsAServiceTemplate.inf "$Sids,*$MySID"

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
secedit /import /db secedit.sdb /cfg "$scriptPath\LogOnAsAServiceTemplate.inf"
secedit /configure /db secedit.sdb

gpupdate /force

The more succinct/elegant option.

# Using one of the modules - just replace the right needed.
Find-Module -Name 'SecurityPolicy' | 
Install-Module -Force

Get-Command -Module 'SecurityPolicy'
# Results
<#
CommandType Name                        Version Source        
----------- ----                        ------- ------        
Function    Add-UserRightsAssignment    0.0.12  SecurityPolicy
Function    Get-SecurityPolicy          0.0.12  SecurityPolicy
Function    Get-UserRightsAssignment    0.0.12  SecurityPolicy
Function    Remove-UserRightsAssignment 0.0.12  SecurityPolicy
Function    Set-SecurityPolicy          0.0.12  SecurityPolicy
Function    Set-UserRightsAssignment    0.0.12  SecurityPolicy
#>

Get-Help -Name 'Add-UserRightsAssignment' -Examples
# Results
<#
    -------------------------- EXAMPLE 1 --------------------------
    
    PS C:\>Add-UserRightsAssignment -UserRightsAssignment SeBackupPrivilege -Identity "Evotec\Administrator"
#>

FYI --- Update for '@Vomit IT - Chunky Mess Style'. Using the PS_LSA.Wrapper

Add-Type @'
    lots of library code here
'@

$LocalUserRights = New-Object PS_LSA.LsaWrapper($env:COMPUTERNAME)

$LocalUserRights | 
Get-Member
# Results
<#
   TypeName: PS_LSA.LsaWrapper

Name                           MemberType   Definition                                                      
----                           ----------   ----------                                                      
AddPrivilege                   Method       void AddPrivilege(string account, PS_LSA.Rights privilege)      
...                     
EnumerateAccountPrivileges     Method       PS_LSA.Rights[] EnumerateAccountPrivileges(string account)      
EnumerateAccountsWithUserRight Method       string[] EnumerateAccountsWithUserRight(PS_LSA.Rights privilege)
...                                                 
RemovePrivilege                Method       void RemovePrivilege(string account, PS_LSA.Rights privilege)   
...
#>

# Examples:

$LocalUserRights = New-Object PS_LSA.LsaWrapper($env:COMPUTERNAME)

$LocalUserRights.AddPrivilege("$env:COMPUTERNAME\$env:USERNAME", "SeBatchLogonRight")
$LocalUserRights.RemovePrivilege("$env:COMPUTERNAME\$env:USERNAME", "SeBatchLogonRight")
7
  • 1
    @VomitIT-ChunkyMessStyle... update provided.
    – postanote
    Commented Nov 25, 2022 at 21:43
  • Oh yeah, now you're talking!!! I saw github examples of that Indented.SecurityPolicy you suggested listed there. I like it! Commented Nov 25, 2022 at 21:55
  • 1
    Yeppers, I've got a bunch of these I've collected, refactored, and written over the years in different engagements. Even one using the underlying OS PS_LSA Windows library.
    – postanote
    Commented Nov 25, 2022 at 22:06
  • Thanks for helping me.The module of 'SecurityPolicy' is available, but when I try to find its modules "Get-Command -Module 'SecurityPolicy'" nothing is listed. Thus, I can't execute 'Add-UserRightsAssignment'. Commented Nov 29, 2022 at 13:38
  • If you did this Get-Command -Module 'SecurityPolicy', and you see nothing? If so, that means it's not installed/in your PSModulePath. Did you install the module as I show in my suggested answer? If not, then you need to. Then you use Get-Module -ListAvailable to validate it's on your system.
    – postanote
    Commented Nov 30, 2022 at 6:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .