Exchange get all users and list names and which database they are located on

At work we sometimes get restore cases on mailboxes where the backup is located on tapes outside of the tape robot. We the have to restore all databases and then manually find which database the user is located on. This is very time consuming and to cut restore time and to make it easier for the technician doing the restore I came up with this script that will export which database the user is located on the specific date. with this information the technician will only have to restore a single database. The script will mail the result as an attachment to one of our function mailboxes and store it locally on the server where the script will run as a scheduled task.

# Filepath with uniqe date.
$date = get-date -Format yyyy.MM.dd
$result = "C:\backup\Resultat_" + $date +".txt"

# Get all mailboxes in Exchange, sort after database and name, select name and database and then export to text file.
Get-Mailbox -Resultsize Unlimited | Sort-Object database, name | Select-Object name, displayname, database | Export-csv $result  -NoTypeInformation

#Mail the text file to
Send-MailMessage -Attachments $result -Subject "Users and Databases Exchange" -Body "Bifogad fil innehaller vilka anvandare som ligger pa vilken databas för tillfallet i Exchange" -To "" -From "" -SmtpServer "" 

Renew certificate on Exchange 2010



The last day before a week of to Spain with the family the boss booked me to consult her on renewing the certificate on our exchange-servers. With little time and no will to work on my vacation, I threw together this script so that she could perform the task herself.

#NAME: new_cert_exchange2010.ps1
#AUTHOR: Viktor Lindström
#COMMENTS: Renew SAN-cert in exchange 2010

# Create request and put the request file in c:\temp\2014.req
$gen = Get-ExchangeCertificate -Thumbprint 'active cert thumbprint' | New-ExchangeCertificate -GenerateRequest -PrivateKeyExportable $true
Set-Content -path "c:\temp\2014.req" -Value $gen

#send content in file to external CA

# Import certificate, on the sam server that created the request.
Import-ExchangeCertificate -FileData ([Byte[]]$(Get-Content -Path c:\temp\frånCA.cer -Encoding Byte -ReadCount 0))

# Check thumbprint on new certificate, the latest not after is the new one.
Get-ExchangeCertificate | select notafter, thumbprint

# Export certificate, run on the server that the certificate is installed on, on thumbprint paste thumbprint from last script-line.
$ExportCert = Export-ExchangeCertificate –Thumbprint 'thumbprint' -BinaryEncoded:$true –Password (Get-Credential).Password
Set-Content –Path C:\temp\2014.pfx –Value $ExportCert.FileData –Encoding Byte

# Import certificate, run on all other exchange-servers that hosts client access.
Import-ExchangeCertificate -FileData ([Byte[]]$(Get-Content -Path c:\certif\2014.pfx -Encoding byte -ReadCount 0)) -Password:(Get-Credential).password

# Activate the new certificate, run on all exchange-servers with the new certificate, same thumbprint as earlier. 
Get-ExchangeCertificate -Thumbprint 'Thumbprint' | Enable-ExchangeCertificate -Services IMAP,POP,IIS,SMTP

# Check if the certificate is enabled.
Get-ExchangeCertificate | where-object {$_.status -eq "valid" -and $ -like "*iis*"} | select notafter, friendlyname