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.
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Jonathan Hassell

Computerworld
Show Comments

Brand Post

PC World Evaluation Team Review - MSI GS75

As the Maserati or BMW of laptops, it would fit perfectly in the hands of a professional needing firepower under the hood, sophistication and class on the surface, and gaming prowess (sports mode if you will) in between.

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Jack Jeffries

MSI GS75

As the Maserati or BMW of laptops, it would fit perfectly in the hands of a professional needing firepower under the hood, sophistication and class on the surface, and gaming prowess (sports mode if you will) in between.

Taylor Carr

MSI PS63

The MSI PS63 is an amazing laptop and I would definitely consider buying one in the future.

Christopher Low

Brother RJ-4230B

This small mobile printer is exactly what I need for invoicing and other jobs such as sending fellow tradesman details or step-by-step instructions that I can easily print off from my phone or the Web.

Aysha Strobbe

Microsoft Office 365/HP Spectre x360

Microsoft Office continues to make a student’s life that little bit easier by offering reliable, easy to use, time-saving functionality, while continuing to develop new features that further enhance what is already a formidable collection of applications

Michael Hargreaves

Microsoft Office 365/Dell XPS 15 2-in-1

I’d recommend a Dell XPS 15 2-in-1 and the new Windows 10 to anyone who needs to get serious work done (before you kick back on your couch with your favourite Netflix show.)

Maryellen Rose George

Brother PT-P750W

It’s useful for office tasks as well as pragmatic labelling of equipment and storage – just don’t get too excited and label everything in sight!

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?