AzureAD/O365 – Update User’s Manager using PowerShell

Organizational hierarchy is vital to run everyday activities smoothly in any enterprise. With Azure AD, user’s manager information can be updated easily and this information can be used in other features like Teams and apps that require workflow.

In this post, I’ll go over steps on how to update manager information for a specific user, for a list of users based on information from a csv file and to determine users’ manager information

Make sure you are connected to Azure AD,

$credential = Get-Credential
Connect-AzureAD -credential $credential

To update manager information for a specific user:

$user = Read-Host "Enter user's email address to update Manager information"
$Manager = Read-Host "Enter Manager's email address"
Set-AzureADUserManager -ObjectId (Get-AzureADUser -SearchString $User).Objectid -RefObjectId (Get-AzureADUser -SearchString $Manager).Objectid

To bulk update users’ manager information from a CSV file:

Note: csv file has UserEmail,ManagerEmail as column headers

$list = Import-csv "C:\tmp\SetManager\UsersManagers.csv"
Foreach($entry in $list) {
        $User = $entry.UserEmail
        $Manager = $entry.ManagerEmail
        $UserObj = Get-AzureADUser -SearchString $User
        $ManagerObj = Get-AzureADUser -SearchString $Manager

        Write-Host Updating $UserObj.DisplayName with $ManagerObj.DisplayName as manager -ForegroundColor Yellow

        Set-AzureADUserManager -ObjectId $UserObj.Objectid -RefObjectId $ManagerObj.Objectid

        Write-Host "Update Complete." -ForegroundColor Green

}

To determine a user’s manager:

$user = Read-Host "Enter user's email address to determine Manager information"
(Get-AzureADUserManager -ObjectId (Get-AzureADUser -SearchString $User).Objectid).DisplayName

To determine all users’ manager information in the Azure AD tenant:

$Users = Get-AzureADUser -All $true

ForEach($User in $Users) {

    $Manager = Get-AzureADUserManager -ObjectId $User.Objectid
    If ($Manager) {
    Write-Host $User.DisplayName manager is $Manager.DisplayName -ForegroundColor Green}
    Else {
    Write-Host "No Manager information found" for $User.DisplayName -ForegroundColor Red}
}

Thank you for stopping by. ✌

PowerShell – Determine all file extensions and count to files by extensions in a folder

Managing file shares can be daunting task some times and when you are trying to make sure the data doesn’t walk out of these shares, other control mechanism usually come into place and along with that comes questions to administrators. One such question I got asked recently was, can you tell me all the file types in this share and how many files there are?

If you come across this situation, you can use the below lines to get an easy overview of the filetypes and the count of files. The input can also be in the UNC format(Ex: \\Server2\Share)

$path = Read-Host "Enter folder path to analyze file extensions"
Get-ChildItem -Path $path -Recurse -File | Group-Object Extension | Sort-Object count -desc | ft -AutoSize

I also put together this below script that will generate two csv files as output. One with a overview of file extensions and count of files and the second with the files’ path on where it is stored in the share.

$path = Read-Host "Enter folder path to analyze file extensions"
$dir = Split-Path -Path $path -Leaf

# create a folder named 'Report' in the script path to store the csv files generated
$ReportFolder = ".\Report"
if (Test-Path $ReportFolder){
    Write-Host "Report folder already exists" -ForegroundColor Green
}
else
{
    New-Item $ReportFolder -ItemType Directory
    Write-Host "Report Folder Created successfully" -ForegroundColor Red
}

#variable to store file name for log file extensions and count. Create top two columns = Extension and File Count
$ft = “.\Report\Folder report for ” + $dir + "$((Get-Date).ToString("MMddyyyy_HHmmss"))" + “.csv”
“Extension`tFile Count” | Out-File -FilePath $ft

#variable to store file name for file name to log file list by extension
$fl = “.\Report\File extension report for ” + $dir + "$((Get-Date).ToString("MMddyyyy_HHmmss"))" +“.csv”

#Analyse path for file type extension
$dirext = Get-ChildItem -Path $path -Recurse -File | Group-Object Extension | Sort-Object count -desc

foreach($ext in $dirext)
{
$ext.Name + “`t” + $ext.Count | Out-File -FilePath $ft -Append

“File Report on file Extensions`:  ” + $($ext.Name) + ” [Count`: ” + $ext.Count + “]” | Out-File -FilePath $fl -Append
$ext.Group.FullName | Out-File -FilePath $fl -Append
“`n`r” | Out-File -FilePath $fl -Append
}
Write-Host "Reports generated successfully" -ForegroundColor Green

Hope this post helped you in digging into what’s in your file shares when it comes to file types.😉😁

Thank you for stopping by. ✌