Skip to content

Tag: PowerShell Code Examples

VMware Tools Version Report Script

In this PowerShell script, we automate the process of retrieving VMware Tools version information for virtual machines (VMs) managed by VMware vCenter Server. The script establishes a connection to the VMware environment, retrieves a list of VMs, and then iterates through each VM to fetch its VMware Tools version and exports to a CSV file.


Ensure you have the VMware PowerCLI module installed:

Install-Module -Name VMware.PowerCLI -Scope CurrentUser

PowerShell Script:

# Define vCenter Server details
$vCenterServer = ""
$username = "your_username"
$password = "your_password"

# Connect to VMware vCenter Server
Connect-VIServer -Server $vCenterServer -User $username -Password $password

# Get a list of VMs
$VMs = Get-VM

# Create an array to store VM information
$VMInfo = @()

# Iterate through each VM to retrieve VMware Tools version
foreach ($VM in $VMs) {
    $ToolsVersion = Get-VMGuest -VM $VM | Select-Object -ExpandProperty ToolsVersion
    $VMInfo += [PSCustomObject]@{
        VMName = $VM.Name
        ToolsVersion = $ToolsVersion

# Export VM information to CSV
$VMInfo | Export-Csv -Path "VMware_Tools_Info.csv" -NoTypeInformation

# Disconnect from VMware vCenter Server
Disconnect-VIServer -Server $vCenterServer -Confirm:$false

Replace "", "your_username", and "your_password" with your actual vCenter Server address, username, and password respectively.

This script will create a CSV file named VMware_Tools_Info.csv in the current directory containing the VM names and their respective VMware Tools versions.

By automating this process, administrators can save time and ensure consistency in managing VMware Tools versions, contributing to the efficiency and reliability of their virtual infrastructure.

Recommended Reading: Automating VMware Tools Installation with PowerShell

Export users from Active Directory to .csv file and email as attachment

Here’s a PowerShell script that exports a list of users from Active Directory, creates a CSV file, and sends it as an attachment with an HTML email body:

# Define variables
$smtpServer = "your_smtp_server"
$from = ""
$to = ""
$subject = "Active Directory Users Report"
$htmlBody = @"
<title>Active Directory Users Report</title>
<p>Below is the list of users from Active Directory:</p>
# Error handling function
function Handle-Error {
    Write-Host "Error: $errorMessage" -ForegroundColor Red
    exit 1
# Export users from Active Directory to CSV
try {
    Get-ADUser -Filter * -Properties * | Select-Object Name, SamAccountName, Email |
    Export-Csv -Path "AD_Users_Report.csv" -NoTypeInformation -Force
} catch {
    Handle-Error "Failed to export users from Active Directory"
# Send email with CSV attachment and HTML body
try {
    $attachment = "AD_Users_Report.csv"
    Send-MailMessage -SmtpServer $smtpServer -From $from -To $to -Subject $subject -BodyAsHtml $htmlBody -Attachment $attachment
} catch {
    Handle-Error "Failed to send email with CSV attachment"
# Clean up CSV file
try {
    Remove-Item "AD_Users_Report.csv" -ErrorAction SilentlyContinue
} catch {
    Handle-Error "Failed to delete temporary CSV file"

For this script to work, it needs to be executed on a machine that is joined to the Active Directory domain or has access to the Active Directory environment. This is because the Get-ADUser cmdlet, which retrieves user information from Active Directory, requires access to an Active Directory domain controller.

Script explanation:

  1. Variables:
    • $smtpServer: SMTP server address for sending emails.
    • $from: Sender’s email address.
    • $to: Recipient’s email address.
    • $subject: Subject of the email.
    • $htmlBody: HTML content of the email body.
  2. Error Handling Function (Handle-Error):
    • This function takes an error message as input and displays it in red color. It then exits the script with exit code 1.
  3. Export Users from Active Directory:
    • Uses Get-ADUser cmdlet to retrieve all users from Active Directory.
    • Selects specific properties (Name, SamAccountName, Email).
    • Exports the result to a CSV file using Export-Csv.
  4. Sending Email:
    • Uses Send-MailMessage cmdlet to send an email.
    • $smtpServer, $from, $to, $subject, $htmlBody, and $attachment are used to compose the email.
    • -BodyAsHtml parameter is used to specify that the body of the email is in HTML format.
  5. Clean Up:
    • Removes the temporary CSV file after sending the email. This ensures that no unnecessary files are left behind.

Recommended Reading: Active Directory Administration with PowerShell – pt. 1

© 2024 - Powered by Coffee & Magic