Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Sunday, April 3, 2022

Basic PowerShell Commands

Get Execution Policy

Get-ExecutionPolicy

Set Execution Policy to Unrestricted

Set-ExecutionPolicy Unrestricted

Show PowerShell Version

$PSVersionTable

Get help for a command
Use this to get the help information for a command
Get-Help <cmdlet-name>

Search Get Help
Use this to search the help files. This is useful if you don’t know the command or want to see if one exists.
Get-Help *keyword*

Get Installed Modules
Use this command to display all the installed modules on a computer 
Get-InstalledModule

List All Available Modules
This will list all available modules on the computer.
Get-Module -ListAvailable

Get Installed Modules
Use this command to display all the installed modules on a computer
Get-Module -ListAvailable

Display available commands
This will display all commands that are available based on the modules that are loaded.
Get-Command


Monday, September 27, 2021

PowerShellTip: Get Get AD Domain Controllers operating system


Get-ADDomainController -Filter * | Select-Object Name, OperatingSystem





PowerShellTip: Get AD Global Catalog status from all DC


 
[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain().DomainControllers | %{"$($_.Name) : $($_.isglobalcatalog())"}

Monday, May 11, 2020

PowerShell Script: Get Inactive AD Users


Simple script to show inactive Active Directory users. Export data to CSV.
Based on Last Logon Time Stamp.

Link for download:
Get-InactiveADUsers-CSV.ps1


Tuesday, April 21, 2020

How to view Msol User Groups in Powershell

Sometimes th administrator needs to know which groups the Office 365 user is member of.
You can use the command Get-MsolUserGroup to resolve this issue.

Installtion
Install-Module -Name Microsoft365.Toolbox

Usage
Import-Module ActiveDirectory.Toolbox

Commands
Get-MsolUserGroup [-UserPrincipalName]





Note:
#Requires -Modules MSOnline

Friday, February 14, 2020

How to view BitLocker Recovery Key in PowerShell


Installtion

PS> Install-Module -Name ActiveDirectory.Toolbox


Usage

PS> Import-Module ActiveDirectory.Toolbox



Commands

Get-ADComputerBitLockerInfo [[-ComputerName] <string[]>]

or

Get-ADComputerBitLocker [[-ComputerName] <string[]>]

Note:
#Requires -Modules ActiveDirectory

How to view LAPS password in PowerShell

Installtion
PS> Install-Module -Name ActiveDirectory.Toolbox
Usage

PS> Import-Module ActiveDirectory.Toolbox




Commands

Get-ADComputerLAPSInfo [[-ComputerName] <string[]>]

or

Get-ADComputerLAPS [[-ComputerName] <string[]>]




Link for download


Note:
#Requires -Modules ActiveDirectory

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"}



Saturday, August 11, 2018

PowerShell Script: Get AD Domain and Forest functional level

Simple script to show Active Directory domain and forest functional level, FSMO roles, all sites in the forest, Global Catalog servers.

Link for download
.\Get-ADInfo.ps1

Example of usage:
[PS].\Get-ADInfo.ps1

Wednesday, July 11, 2018

PowerShell Script: Remove AD Groups for Users in specific OU

This small script remove all Active Directory (AD) groups for users in specific Organizational Unit (OU).
Except group "Domain Users"
 
Example of usage:
[PS].\Remove-ADGroups-for-Users-in-OU.ps1 -OU "OU=Users,OU=Disabled Accounts,DC=myDomain,DC=local" -Confirm:$False
 
Script can be useful for OU with disabled users accounts.
 

PowerShell Script: Get LAPS Password Information from Active Directory

A small script for export Computers LAPS Password information from Active Directory to csv file.
Script generates a CSV file with computer names and LAPS Passwords.
    ComputerName;OperatingSystem;Password;PasswordExpTime;DistinguishedName
Requirement of the script:
   - Active Directory PowerShell Module
   - Needed rights to view AD LAPS Attributes: ms-Mcs-AdmPwd, ms-Mcs-AdmPwdExpirationTime
  
Example of usage:
[PS].\Get-ADComputers-LAPS-Password.ps1
[PS].\Get-ADComputers-LAPS-Password.ps1 -OU "OU=Computers,OU=IT Department,DC=myDomain,DC=com"

Links for download
PowerShell Gallery | Get-ADComputers-LAPS-Password
GitHub | Get-ADComputers-LAPS-Password.ps1
Get-ADComputers-LAPS-Password.zip

PowerShell Script: Get BitLocker Recovery Information from Active Directory

A small script for export Computers BitLocker Recovery Information from Active Directory to csv file.
This script generates a CSV file with computer names and BitLocker Recovery Keys:
    ComputerName;OperatingSystem;Date;Time;GMT;PasswordID;RecoveryPassword;DistinguishedName
Requirement of the script:
    - Active Directory PowerShell Module
    - Needed rights to view AD BitLocker Recovery Info

Example of usage:

[PS].\Get-ADComputers-BitLockerInfo.ps1
[PS].\Get-ADComputers-BitLockerInfo.ps1 -OU "OU=Computers,OU=IT Department,DC=myDomain,DC=com"

Wednesday, June 13, 2018

PowerShellTip: Use PowerShell to Find User Profile Path

To easily get information about the folder and path to the profile for a currently signed-in user, use environmental variable UserProfile.

[PS]$env:UserProfile

This can be useful in scripts.

Friday, January 5, 2018

PowerShellTip: Get Computer Name

To easily retrieve computer name in Windows PowerShell, use the environmental variable ComputerName.

[PS]$env:ComputerName

This can be useful in scripts.

Friday, November 24, 2017

Get Active Directory Schema Version from PowerShell

To view Active Directory Schema Version from PowerShell use command
 
[PS]Get-ADObject (Get-ADRootDSE).schemaNamingContext -properties objectVersion
 
 
 
 

Thursday, June 8, 2017

Tips of the day #1706

Tip of the day #1706-1

Use Windows PowerShell to change the UPN suffix for all users
Change all company.local suffixes to company.com

Import-Module ActiveDirectory

Example 1.
$LocalUsers = Get-ADUser -Filter {UserPrincipalName -like '*company.local'} -Properties userPrincipalName -ResultSetSize $null

$LocalUsers | foreach {$newUpn = $_.UserPrincipalName.Replace("company.local","company.com"); $_ | Set-ADUser -UserPrincipalName $newUpn}