On Windows 10, you can create a local user account, or you can create an account that is connected to a Microsoft account to take advantage of additional benefits, such as settings syncing across devices and seamless integration to various Microsoft cloud services.

1. Open the PowerShell with Administrator access in Windows.

2. To create the local user or service account using PowerShell script.

3. Here in my case, I am checking whether user accounts exist are not using CMatch if it does not exist then it will add to the server.

#Function that logs a message to a text file
$LogFile = "<%= @dir %>\psLogserviceaccount.log"

#Delete log file if it exists
if(Test-Path $LogFile)
{
Remove-Item $LogFile
}

#Start Logging
Start-Transcript -Path $LogFile
#Code starts executing from here
$serviceaccount = (Get-LocalGroupMember -Group Administrators | Where {$_.Name -CMatch "service_account"})

if ($serviceaccount.count -eq 0){
Write-host "Adding Both Service Accounts" -foregroundcolor "white" -backgroundcolor "red"
net localgroup Administrators DBADEED\service_account1 /add
net localgroup Administrators DBADEED\service_account2 /add
}
else
{
Write-Host "Check Primary User DBADEED\service_account1 " -foregroundcolor "white" -backgroundcolor "green"
if ($serviceaccount.Name -eq 'DBADEED\service_account1') {
Write-Host "user exists"
}
else {
Write-Host "user not exists! So Adding Back to Server" -foregroundcolor "white" -backgroundcolor "red"
net localgroup Administrators DBADEED\service_account1 /add
}
Write-Host "Check Secondary User DBADEED\service_account2 " -foregroundcolor "white" -backgroundcolor "green"
if ($serviceaccount.Name -eq 'DBADEED\service_account2') {
Write-Host "user exists"
}
else {
Write-Host "user not exists! So Adding Back to Server" -foregroundcolor "white" -backgroundcolor "red"
net localgroup Administrators DBADEED\service_account2 /add
}

}
#Stop Logging
Stop-Transcript

Leave a comment