Based on your description, here are test in my lab
The source file that I used:
Test the first part of script, check whether it could find correct users:
$users = import-csv C:/temp/users.csv
$Data = @()
foreach ($user in $users){
$UserName = $user.name +" "+$user.Surname
Get-ADUser -Filter 'Name -eq $UserName' | select UserPrincipalName
}
Now, we could add the export request command to this script:
$users = import-csv C:/temp/users.csv
$Data = @()
foreach ($user in $users){
$UserName = $user.name +" "+$user.Surname
$temp = Get-ADUser -Filter 'Name -eq $UserName' | select UserPrincipalName
$fileName = $temp.UserPrincipalName.split('@')[0].tostring()
New-MailboxExportRequest -Mailbox $temp.UserPrincipalName -Name $fileName -FilePath \\EXCH\Shared\$fileName.pst
}
About disable mailbox, I would suggest you use a separate script for it. It is suggest to contained them in one script(because the export request need take time to running):
$users = import-csv C:/temp/users.csv
$Data = @()
foreach ($user in $users){
$UserName = $user.name +" "+$user.Surname
$temp = Get-ADUser -Filter 'Name -eq $UserName' | select UserPrincipalName
$fileName = $temp.UserPrincipalName.split('@')[0].tostring()
If ((Get-MailboxExportRequest $fileName).Status -eq "Completed"){
Disable-Mailbox $temp.UserPrincipalName -confirm:$false
}
}
For more professional script, it is suggested to open a ticket to Microsoft.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.