Month: April 2010

ConvertTo-PowerShell: PowerShell versus “other” scripting examples – Part 5: List 20 largest files

In today’s post, I will show an example script to retrieve a 20 largest files on C: drive. As usual, we will first look at the VbScript example and then see a PowerShell “one-liner”. VBScript

This VBScript uses log parser 2.2 COM object to make it easy and faster to get a list of largest files. It would be very tough to achieve this without log parser and using file system object. So, to be able to run this script, you must have log parser 2.2 installed on your system. Now, let us see how we can do that in PowerShell without requiring log parser and in just one command.

This is it. It is simple and concise. That is the Power of PowerShell.

ConvertTo-PowerShell: PowerShell versus “other” scripting examples – Part 4: Renaming files

Continuing this series of posts on PowerShell-ism, today, I will show a VBScript example and then convert it to a PowerShell one-liner. So, this script was my response to a question on the scripting guys forum. This script reads a text file containing tab delimited information and then uses the lines in the text file to rename a bunch of files. So, let us jump in. VBScript


This is it. I am sure you must be seeing the benefits of “converting” to PowerShell-ism by now.

ConvertTo-PowerShell: PowerShell versus “other” scripting examples – Part 3

Yesterday, I mentioned that PowerShell can take care of many important aspects like formatting output, etc and let you focus on the core aspects of what you want to achieve using a PowerShell script. In today’s post I will show an example to demonstrate this.   In this example, let us first look at a VBScript that gets a list of processes running on a set of remote computers and then we willl look at how PowerShell simplifies that.   VBScript  

Look at this VBScript. We loop through the contents of a text file, pickup each line (a server name) as strComputer, connect to the computer using WMI, retrieve a list of processes and then finally, iterate through the process object to display list of properties we need. Many steps involved in doing this that require you to have knowledge of many others such as WMI. The output formatting is done by us. Here we just pickup three properties of a process object. Imagine writing up Wscript.Echo statement for all the 20+ object properties. Also, we are not really formatting …

ConvertTo-PowerShell: PowerShell versus “other” scripting examples – Part 2

Please take a look at part 1 to understand why I started this series of articles.  In this post, I will show another example from the scripting guys forum. Again, this is a PowerShell one-liner to do the job of 30 lines of VBScript. What this script does is real simple. It just creates a folder with current time as name and inside that a folder with current date as name. Simple..right.!?       VBScript      

As you see in the above example, we spend more time creating objects, parsing date / time, etc. Where as, the core requirement is a simple CreateFolder method.       PowerShell  

It looks simple in PowerShell because of it’s implementation of .NET based self-describing object model. We check if a folder with a specified name exists and if not, we create the folder and pipe the resulting object to ForEach-Object and use the CreateSubDirectory method to create a folder underneath it.     PowerShell lets you concentrate on what you needs to achieve and it takes care of remaining aspects like formatting, etc. More on this in the …

SharePoint 2010 and PowerShell annoyances – Part 1: SharePoint backup

Microsoft and SharePoint product teams certainly did a good job by enabling out-of-box PowerShell management of SharePoint. It really provides some great power to IT professionals and administrators managing SharePoint farms. But, in my opinion, this is not the best implementation of PowerShell support. I will dig in to why I feel like that by quoting a few examples in each of these blog posts. Yes, I know, I could have written all this as product feedback during beta. But , nevertheless, I hope this reaches the product team at MS. I will start this series with a couple of SharePoint 2010 backup cmdlets. To perform SharePoint 2010 backup, there are actually 3 cmdlets — Backup-SPFarm, Backup-SPSite, and Backup-SPConfigurationDatabase. Backup-SPFarm Backup-SPFarm -BackupMethod <String> -Directory <String> [-AssignmentCollection <SPAssignmentCollection>] [-BackupThreads <Int32>] [-ConfigurationOnly <SwitchParameter>] [-Confirm [<SwitchParameter>]] [-Force <SwitchParameter>] [-Item <String>] [-Percentage <Int32>] [-WhatIf [<SwitchParameter>]] What is wrong with this cmdlet?  In short, many things..! 1. There is no -AsJob parameter. I know it requires PS remoting infrastructure but I don’t want to wait at the console until full backup …

ConvertTo-PowerShell: PowerShell versus “other” scripting examples – Part 1

On the scripting guys forum and various other places, I still see various people asking for VBScript examples or DOS batch file examples for achieving various things on Windows OS. I — almost every time (unintentionally) — provided a PowerShell example even when the person’s preference was VBScript or DOS batch. I see many people shying away from Windows PowerShell thinking that the language is too complex. It is actually not “so” complex. I agree that it takes time to get used to it.  Windows PowerShell is not just an alternative to other scripting languages on Windows platform. If you think it is, time for you to change that thought process. In my opinion, being an alternative scripting language is a good side-effect of being the most powerful scripting platform. It is time people start using PowerShell or convert to the religion called PowerShell-ism.  One way to showcase the power of PowerShell is to show how easy it is to script it PowerShell and show how concise these scripts can be. So, that is the goal of this blog series. …

One click import of PowerShell module-sets from an ISE addon menu

It has been a while since I last blogged and also there are several PowerShell projects I kept on hold. One of that is an ISE addon to import module-sets from an addon menu option. By saying module sets, I mean a group of dependent PowerShell modules required to achieve a specific task. You can achieve that by simply loading all those modules manually. But that is not how “PowerShellers” do their work. We — PowerShellers — automate such things and that is the idea behind module sets. For example, this is what I have in my PowerShell profile.

So, in the above code snippet, all I am doing is adding an ISE addon menu option and loading a set of modules/snapins when I select that option. Simple.! This would show up in my ISE environment as this   Now, when I select SharePoint all the above modules get loaded. BTW, SPISE is yet another ISE addon I am working on to provide some cool extensions to SharePoint 2010 cmdlets. Stay tuned. It will come out soon.  But creating …

PowerShell file browser to demonstrate Windows forms ListView control

It has been a while since I blogged something here. I have been busy with some work and not able to spend time writing on this blog. Anyway, today, I want to share a demo script I created to demonstrate using ListView control within PowerShell. I created this a while back and did not really get a chance to write about it. I made a few updates to the GUI and just published it on TechNet script gallery. Here is how it looks.    This one is really straight-forward to create. There is no complex stuff involved as in the case of DataGrid or TreeView controls.  First step is to use Sapien PrimalForms to create the GUI form and add the necessary controls as needed. The scripting guys blog has a pretty good article on this.  Windows forms listview supports different modes and in this example we are only looking at the LargeIcon view. Similar to Windows explorer, we can provide an option to the end user to select the view they want and refresh the contents …