Hello readers,
In this post we will create a short script in which I will combine almost all the things that we’ve learned so far. It is not a complicated script and you will see that it is pretty easy to understand.
I have written comments all over the script to make the script easier to understand but if you have any misunderstanding please leave a comment and I will respond as soon as I can.
#******************************************************************************
#Script Name : Active Directory Users
#Description – this script was created to interact with Active Directory users
#Version : 1.0
#Created by : Popescu Dan-Alexandru
#Name : ADusr
#******************************************************************************
# If you have problem running the script write the following : Set-ExecutionPolicy unrestricted
# you can have problems running scripts if your workstation is configured not to accept malicious scripts
#******************************************************************************
#Variables that are used in the script are initialised
[int]$useranswer=0 # stores user answer
#Option1 variables
[string]$path # path to organizational unit
[string]$exportpath # export path
[string]$opt1ans # answer from option 1
[string]$selcolumn #select column answer
#This is the first welcome screen when you run the script
function Welcome_screen
{
Write-Host “`n`n`n`n`t`tW E L C O M E T O T H E”
Write-Host “`n`t`t` Active Directory script”
Write-Host “`n`t`t`t By Popescu Dan-Alexandru”
Write-Host “`n`t`t`t`tPress Enter to continue.”
Read-Host
Clear-Host
}
#this function retrieves the users from active directory, you have to write the path to the OU
Function Get_users
{
Clear-Host
Write-Host “Enter path to objects:”
Write-Host “`n NOTE”
Write-Host “`n CN: common name”
Write-Host “`n C: country”
Write-Host “`n DC: domain”
Write-Host “`n OU: organizational unit”
Write-Host “`n O: organization”
Write-Host “`n C: country”
$path=read-host
#example OU=Users,DC=popesq,DC=ro
Get-ADUser -Filter * -SearchBase $path
}
#This function displays the options that you cand choose
function Show_answers
{
Clear-Host
Write-Host “`n`nPress 1) – export ADusers into a csv or display them”
Write-Host “`n`nPress 2) – quit”
$useranswer=Read-Host “`n`n`n1/2”
if ($useranswer -eq “1”)
{
Clear-Host
Write-Host “Want to export users in a file (Y) or just display them(N) ?”
$opt1ans=Read-Host “`n`n`n(Y/N)”
if ($opt1ans -eq “Y”)
{
Write-Host “Enter path to export:”
$exportpath=read-host
Get_users |Select-Object -Property Surname,SamAccountName | Export-Csv $exportpath
#Select one or multiple fields from below and separate them with comma:”
#DistinguishedName”
#nEnabled”
#nGivenName”
#nName”
#nObjectClass”
#nObjectGUID”
#nSamAccountName”
#nSID”
#nSurname”
#nUserPrincipalName”
break
}
else
{
if ($opt1ans -eq “N”) # if you just want to see the users without exporting
{
Get_users
}
}
}
elseif ($useranswer -eq “2”) # if you want to exit the script
{
Clear-Host
Write-host “ `n`n Thank you the script will exit”
exit
}
else
{
Write-Host “`n`n Select an option”
}
}
#Main program
cd $pshome #sets the powershell home directory
cd modules
Import-Module ActiveDirectory #imports active directory module
Welcome_screen # runs the function welcome_screen
Show_answers #displays the answer
OK, this is our script,now I will show you this script in action:
First save the script with the name ADusr then press right click and press “Run with PowerShell”.
The welcome screen appears, press enter:










































































