Based on lastlogon of a user I want to run commands againist users that lastlogon are older then 30days

I am trying to automate the process for a returning user from extended leave
Here is what i want to happen in this script

1 input the returning users

2 search AD to find user

3 (A) if they are gone more then 30 days their mailboxes are archived I need to filter out those users so that I can run a mailbox restore script and then enable user and move to proper OU

3(B) if less then 30 then enable and move the user to their proper OU

The issue I have is Part 3(A) i can manage the rest.

how to get the username from the output of this
Get-ADUser -Filter {Enabled -eq $False} -Properties name,sAMAccountName,lastLogonDate | Where-Object {$_.lastLogonDate -le [DateTime]::Now.AddDays(-30)} | Select name,sAMAccountName,lastLogonDate | Sort-Object name

so the I can use the samacct to find the old archive mailbox and copy into the new mailbox that we created when we re-enable the user The process for the mailbox part to the I have a script for see below

####Restore a Mailbox of returning user###

NOTE: I did this method because Creating/enabling the account and assigning a license creates a new mailbox. I restored to this new mailbox

  1. Find the inactive Mailbox using link below (most recent date) and copy GUID.

Login details hidden

  1. Set the values of DELETED Mailbox user you want to restore into Variable $InactiveMailbox ####

$InactiveMailbox = Get-Mailbox -InactiveMailboxOnly -Identity PASTEINACTIVEGUIDHERE

  1. Find ExchangeGuid of existing mailbox by using command

Get-Mailbox -Identity “” | Format-List

####Partial Output:
####CalendarRepairDisabled : False
####ExchangeGuid : 44e0511b-e5a8-4d9a-989f-9e1d4812f1c1 < Copy this value from results
####MailboxContainerGuid :

  1. Restore the Deleted mailbox to the new empty mailbox

New-MailboxRestoreRequest -SourceMailbox $InactiveMailbox.DistinguishedName -TargetMailbox PASTECOPIEDGUIDOFRETURNINGUSER -AllowLegacyDNMismatch

  1. If impatient: Check status of restore request


Any help that I can get would be greatly appreciated


Welcome to the forum. :wave:t4:

I actually have a hard time getting what your actual question is. And it is even harder than necessary since you didn’t format your ocde as code.

So when you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in <---- Click :point_up_2:t4: :wink:

What exactly do you mean with …


The output contains the sAMAccountName. Isn’t that what you’re after?

And BTW: The link you posted requires a login. Please do not post those links. :wink:

I have figured this out myself

Thanks to all who have helped

Would you like to share your solution here? This may help others having the same or a similar issue and coming here for help.

Thanks in advance.

My solution was to export the output to csv and use that CSV for the next step

Would you like to share your code? We may be able to optimize it even further. Using an intermediate CSV file sounds cumbersome. :wink:

1 Like