PowerShell Tips and Tricks

At last, Windows has a powerful scripting environment. Here are five real-world ways to put PowerShell through its paces

3. Control Active Directory in PowerShell

PowerShell can interact directly with Active Directory, reading into its database, extracting information and displaying it for you. For example, with the following cmdlet, you can display all users in Active Directory:

$Dom = 'LDAP://DC=scribnertechmediacorp;DC=local'

$Root = New-Object DirectoryServices.DirectoryEntry $Dom

$i=0

# Create a selector and start searching from the Root of AD

$selector = New-Object DirectoryServices.DirectorySearcher

$selector.SearchRoot = $root

$selector.set_pagesize(1000)

$adobj= $selector.findall() |`

where {$_.properties.objectcategory -match "CN=Person"}

foreach ($person in $adobj){

$prop=$person.properties

$i++

Write-host "First name: $($prop.givenname) " `

"Last Name: $($prop.sn) Display Name: $($prop.cn)"

}

"Total AD Users Found: $i"

To query a specific part of Active Directory - for example, a specific Users container - just use a new LDAP path as the value of the $Dom variable. (And did you see the "foreach" loop in there?)

Querying is nice, but what about creating objects? PowerShell can do that, too. To create an organizational unit called EastCoast in the domain above, use the following (replace "mercury," which happens to be my domain controller (DC), with the name of a DC in your organization):

$Dom = [ADSI]"LDAP://mercury:389/dc=scribnertechmediacorp;dc=local"

$newOU = $Dom.Create("organizationalUnit", "ou=EastCoast")

$newOU.SetInfo()

You'll probably want to create new users in this new organizational unit, too. Let's create an account for Karen Smith:

$newOU = [ADSI]"LDAP://mercury:389/ou=eastcoast;dc=scribnertechmediacorp;dc=local"

$newUser = $newOU.Create("user", "cn=KSmith")

$newUser.Put("sAMAccountName", "KSmith")

$newUser.SetInfo()

PowerShell, and its hooks into Active Directory Services Integration (ADSI), provide a great environment for scripting Active Directory administration.

4. Manage your network with PowerShell

PowerShell is eminently more flexible at managing network connections than the tools provided for the Windows command line. For example, you can easily retrieve the MAC address of all network adapters on your current machine with the following script.

$strComputer = "."

$colItems = get-wmiobject -class "Win32_NetworkAdapterConfiguration" `

-computername $strComputer | Where{$_.IpEnabled -Match "True"}

foreach ($objItem in $colItems) {

write-host "Hardware Address:" $objItem.MACAddress

}

You can replace the $strComputer variable with any host name that can be reached by the machine running PowerShell, making it easy to get a list of MAC addresses from around your network.

You can also see what IP addresses are active on your network, using a simple Ping cmdlet that rotates through all possible IP addresses on your local subnet. This will return a table with IP addresses and status codes, 0 indicating that the address is in use.

1..254| ForEach -Process {WmiObject -Class Win32_PingStatus -Filter ("Address='192.168.16." + $_ + "'") -ComputerName .} | Select-Object -Property Address, StatusCode | ft

PowerShell is great for interacting with your network, be it through ping status reports, or through the use of Windows Management Instrumentation (WMI) objects that find out information about network hardware in use.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

More about INSMicrosoftNICESkype

Show Comments