Wednesday, November 20, 2019

How to Check for Administrator Privileges in a PowerShell Script?

If you need to run PowerShell script with administrator privileges, you can directly check the current process for administrative privileges directly in the PS code.

The following PowerShell code can be used to check if the current script is running in “Run as Administrator” mode or not:

function Check-PSAdminPermission (){
   if (-NOT ([Security.Principal.WindowsPrincipal]`
      [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
      [Security.Principal.WindowsBuiltInRole] "Administrator")) {
         Return $false
   }
   else {
         Return $true
   }
}
 
Write-Host "Checking for administrative privileges …"
if (Check-PSAdminPermission) {
    Write-Host "Administrator permission detected" -ForegroundColor Green
}
else {
   Write-Host "Not enough rights to run this script `nOpen the PowerShell console with administrator privileges and run the script again" -ForegroundColor Yellow
    Break
}
 
Write-Host "Continue the script..."

Tuesday, August 20, 2019

PowershellTip: Show Installed Windows Roles and Features

List all installed Features and Roles

Example 1:
 
Get-WindowsFeature | Where-Object {$_. installstate -eq "installed"}
 

Example 2:
 
Get-WindowsFeature | where-object {$_.Installed -eq $True} | Format-List DisplayName
 
 
 
Example 3:
 
Get-WindowsFeature | Where-Object {$_. installstate -eq "installed"} | Format-List Name, Installstate
 
 

List all roles and Features on a Remote Host

Get-WindowsFeature -ComputerName <ComputerName> | Where-Object {$_. installstate -eq "Installed"}
 
 
 
 

Friday, August 16, 2019

PowerShellTip: Move AD Group to another OU

 
Import-module ActiveDirectory
Get-ADGroup MyGroup | Move-ADObject -identity {$_.objectguid} -TargetPath 'ou=GroupsToDelete,dc=MyCompany,dc=com'

Thursday, June 13, 2019

How many users are in AD group?

Some times I need to check how many users are in a particular AD group, unfortunately ADUC doesn't show this info.
So I using Powershell to check this group info

Import-Module ActiveDirectory
$ADGroup = Get-ADGroupMember "Domain Users" -recursive
$ADGroup.Count



Thursday, April 18, 2019

Manage Office 365 Groups (Unified Group) Email Aliases

When working with Office 365 Groups sometimes we need to change email alias in some reason.
We can change Office 365 Groups email alias via PowerShell.


View Office 365 Group email address

To view Office 365 Group email alias 

via PowerShell use command

[PS]Get-UnifiedGroup -Identity <UnifiedGroupIdParameter> | fl EmailAddresses, PrimarySmtpAddress

or

[PS]Get-UnifiedGroup -Identity <UnifiedGroupIdParameter> | select -ExpandProperty EmailAddresses


Add email alias to Office 365 Group

Use this cmdlet to add email alias for Office 365 group:

[PS]Set-UnifiedGroup -Identity <UnifiedGroupIdParameter> -EmailAddresses @{Add="EmailAddress"}



Sunday, February 17, 2019

Clearing the Synchronization history in the WSUS console


When you using WSUS for a long time and go into admin console to the Synchronization node - it loads very long time.
I’m talking about:


You can clean the WSUS synchronization history from database.