My Donet Web Mail Contact Us Arrow

Blog

Usefulness of Windows Powershell

Posted on by Eric Wrightcomment bubble2 Comments

 

For those Windows Admins that haven’t tampered with Windows PowerShell I recommend you start.  I am an amateur when it comes to shells, command line interfaces (CLIs), and scripting languages. However, I am finding the more I dive in the less I want to come up for air.  

Introduction

Powershell is a command-line shell that is also a scripting language. It is integrated with and based on the .NET framework and executes cmdlets (.NET classes).  Since Microsoft often develops it’s own applications using .NET classes we, as system administrators, can call on these same classes using Windows Powershell.  Furthermore we can script these activities to perform functions on a periodic basis.  Some applications examples would be Microsoft Exchange 2007 and SQL Server 2008. 

First-Timer?

If you are a first timer the ‘get-help’ command will be very useful.  The documentation that comes with powershell is phenomenal.  If you are unsure of the help terminology you can take it one step further by issuing a command to retrieve examples of the command.

For example this is a command to retrieve information about the “where-object” cmdlet:

get-help where-object
To retrieve examples:
get-help where-object -examples

Archiving older files
If I administer a file server and need to find ALL older files which have not been accessed in a certain amount of time (in this case before 2006) I can issue the following command:

get-childitem f:\ -Recurse | select-object name,lastaccesstime | where-object {$_.lastaccesstime -lt “1/1/2006″}

Cleaning old .MP3 Files
Every file server that user’s access contain unnecessary files. If you need to clean up .mp3 files that haven’t been listened to in years you can issue this command:

get-childitem c:\ -Recurse | where-object {$_.lastaccesstime -lt “1/1/2007″} | where-object {$_.extension -eq “.mp3″} | del

Other Administrative Tasks
1. Finding Largest / Biggest Directories
Do you often wonder what are the biggest directories on your server? You can find the biggest or largest directories and sort them accordingly by using an application called disk usage (du) provided by Microsoft in the links below. To utilize this utility download “du.exe” and place it in your system32 folder. You can then issue the following command on any volume:

du -v c:\ -recurse | sort -desc | select -first 10

Note that the first 5 lines are descriptors; therefore if you want to see the top 15 directories you can use “select -first 20″ and so on.

2. AD integrated tasks
Powershell can access AD attributes similar to VB scripts. For example if you need to list an active directory object attributes we can issue this command: (courtesy of my co-worker Eric Westfall)
dsquery * “CN=Comman Name,OU=Users,DC=donet,DC=com” -scope base -attr *

Furthermore we can assign this object to a variable by using the following command:
$objuser = [ADSI](“LDAP://CN=Comman Name,OU=Users,DC=donet,DC=com”)

Now we can assign any attribute of this object via the command line.

$objcreated = $objuser.WhenCreated

You can do this massively using arrays utilizing the ForEach function for various different functions.

3. Exchange Management Shell
An example of an exchange shell command to see all mailbox sizes in all databases would be:
get-mailbox | get-mailboxstatistics | select-object DisplayName, TotalItemSize, StorageLimitStatus, LastLogonTime

Conclusion
Many of you already have extensive experience using vb scripts with Windows Server 2003. Powershell’s syntax is slightly different but calls along many of the same classes. Powershell provides a unanimous platform for Microsoft and is the scripting tool they are using to unify their operating system/application management. As a Windows Systems Administrator I am excited to learn more about a tool that I can use to manage both the operating system and the applications to reduce administrative time.

Links to Powershell Resources
Download Powershell
http://www.microsoft.com/windowsserver2003/technologies/management/powershell/download.mspx

Script Repository
http://www.microsoft.com/technet/scriptcenter/scripts/msh/default.mspx?mfr=true

What Can I Do with Windows Powershell
http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/index.mspx

Disk Usage Utility
http://technet.microsoft.com/en-us/sysinternals/bb896651.aspx

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
 
quote

David Mezera

President and Founder

A company is only as good as the people who work there. And in Donet's case, those people are simply exceptional. Bright. Inventive. Rock-solid.quote end

quote

Raleigh Sandy III

Vice President, Founder

You can expect that when you have
a problem, we're going to respond
to it. Quickly. Personally. And effectively.quote end

Social Header