Nazaudy, a spark in your curious mind

Useful powershell scripts

These Useful powershell scripts can help you manage your environment:

 

Robocopy to copy content from A to B

 

 

User account locks

Import-Module activedirectory
$users = Search-ADAccount -LockedOut | Select-Object -ExpandProperty SamAccountName 
ForEach ($user in $users)
{
$test = Get-AdUser $user -Properties * 
$Body = $test.CN + " " + "account was locked out. It is now unlocked"
Send-MailMessage -To $test.Manager -Subject "Below Users LAN ID is locked" -SmtpServer "smtp.gmail.com" -From "This email address is being protected from spambots. You need JavaScript enabled to view it."  -Body $Body
}
Search-ADAccount -LockedOut | Unlock-ADAccount

 

 

 

 

PowerShell to get list of members in an Office 365 Distribution List:

  1. First of all install PS 7.4.4 on your desire computer by visiting this link: https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.4#msi
  2. Run "pwsh.exe"
  3. Execute: Install-Module -Name ExchangeOnlineManagement 
  4. Execute: Connect-ExchangeOnline
  5. Execute: 
    Get-DistributionGroup -Identity "This email address is being protected from spambots. You need JavaScript enabled to view it." -ResultSize Unlimited | Select Name, PrimarySMTPAddress, RecipientType | Export-CSV "C:\Temp\Distribution-List-Members.csv" -NoTypeInformation -Encoding UTF8

 

Get list of members from a Distribution Group

Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName This email address is being protected from spambots. You need JavaScript enabled to view it.
Get-AdGroupMember -identity "MFA-AD-GROUP" | select name > list.txt

Get-DistributionGroupMember -identity "Exchange Distribution List" | format-list
Add-DistributionGroupMember -identity "Exchange Distribution List" -member "New User 1"

get-adgroup -filter * | sort name | select Name