Power Shell Scripts

# Sometimes we need to get the host name of PDC then we can use this code/script

$domainController = ([System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()).DomainControllers[0].NameWrite-Host “Domain Controller Hostname: $domainController”

# To get the all groups and members details from Active directory using Power Shell script and output will be store in csv file

Import-Module ServerManager
Add-WindowsFeature RSAT-AD-PowerShell

# Import the Active Directory module
Import-Module ActiveDirectory

# Specify the distinguished name of your domain
$domainDN = "DC=test,DC=com"

# Create an array to store results
$results = @()

# Get all groups in Active Directory
$groups = Get-ADGroup -Filter *

# Check if any groups are found
if ($groups.Count -eq 0) {
    Write-Host "No groups found in Active Directory."
}
else {
    # Iterate through each group and get its members
    foreach ($group in $groups) {
        $groupName = $group.SamAccountName
        $groupInfo = New-Object PSObject -Property @{
            'GroupName' = $groupName
            'Members'   = ""
        }

        Write-Host "Processing Group: $groupName"

        # Get group members using the Active Directory module
        $groupMembers = Get-ADGroupMember -Identity $groupName

        # Check if any members are found
        if ($groupMembers.Count -eq 0) {
            Write-Host "  No members found for group: $groupName"
        }
        else {
            $memberNames = $groupMembers | ForEach-Object {
                $_.SamAccountName + " (" + $_.Name + ")"
            }

            $groupInfo.Members = $memberNames -join ', '
            Write-Host "  Members: $($groupInfo.Members)"
        }

        # Add the group information to the results array
        $results += $groupInfo
    }

    # Export the results to a CSV file
    $csvPath = "C:\temp\OutputFile.csv"
    $results | Export-Csv -Path $csvPath -NoTypeInformation

    Write-Host "Script Execution Completed. CSV file has been created at: $csvPath"
}



Get-ADGroupMember -Identity "Domain Admins" | Select-Object SamAccountName,Name | Export-Csv -Path "C:\Temp\OutputFile.csv" -NoTypeInformation

# PowerShell command to get the OS and Hardware complete details

“Get-ComputerInfo”