I seem to find my self turning to regular expressions more often these days, often to help out a fellow scripter in one of the many scripting forums I keep tabs on. I thought I’d take a moment and provide a brief explanation on how to use regular expressions in VBScript. First off, for those who don’t know, a regular expression is a way of identifying or matching a string against a pattern. For example we all know that 172.16.100.200 looks like an IP address because of its pattern. The string roygbiv@rainbow.net looks like an email address, again based on the pattern. A regular expression can be used identify strings based on a pattern.
Tag: scripting
Get Local Member
I’ve been dealing with the topic of enumerating local group membership in several PowerShell forums from different people. My responses have pretty much been the same. But if these people are asking its a safe bet there are others who are also in need. The bottom line has been to query a local group, typically Administrators, and return a list of group members. If the member is a domain account, then get a little extra information. I put together a script called Get-Localmember.ps1 that I think will get the job done for most of you.
Up another PowerShell Tree
A few weeks ago I posted a PowerShell script that created a directory tree, like the TREE command from the CMD shell. Then while testing something new in PrimalForms, I realized I had a literal form object, a tree view, that I could use. So I put together a short script that creates a graphical tree for a given directory, along with the file counts and sizes from my original scripts. As a bonus, because I now had a graphical interface I could do things such as display empty folders in a different color. The script not only gives you a handy tool but also serves as a demonstration on how to use a TreeView control.
Out-CSV
Sometimes it’s the little things that can make life easier. Here’s one of them. We know that PowerShell treats comma separated items as an array:
PS C:\> $a=”a”,1,”b”,”jeff”,4
PS C:\> $a
a
1
b
jeff
4
PS C:\>
But what if you want back in its original comma delimited list? Or suppose you wanted to create a comma delimited line of all running services? Export-CSV isn’t really going to help in these situations. I came up with function called Out-CSV.
PowerShell Tree
Of course since I’ve posted a VBScript version of the Tree command, I had to do it in PowerShell. On one hand, this would have been pretty straightforward since PowerShell can use COM objects and I could have simply reused the FileSystemObject. But that didn’t feel right. So I came up with a version that uses cmdlets and PowerShell features like the -f formatting operator. So here is TREE.PS1.
Up a Tree
One of the first CMD commands I fell in love with (yes I’m that geeky) was TREE. When executed from a command prompt it would give you a nice graphical representation of the directory structure. Never being satisfied I wrote a VBScript version years ago. I decided to dust it off and tweak it some. Here is TREE.VBS.
Registration for Intermediate PowerShell Ends soon
I’m in the middle of running our online PowerShell Fundamentals class. The Intermediate class starts April 6. Registration ends on March 30th, or until the class fills up. Class size is limited. You can register for the class at ScriptingOutpost.com.
This course covers intermediate topics such as error handling, debugging, regular expressions, advanced modularization techniques, advanced object manipulation, and much more. You’ll be a more refined and efficient administrator by leveraging more of PowerShell’s flexible and powerful capabilities.
Show-CSpace
My latest Prof. PowerShell column has a function that takes a computername as pipelined input. It then returns a custom object showing free space on drive C:\. The point of the column was to demonstrate how to make a function behave like a cmdlet and take pipelined input so it’s not necessarily the most glamorous function I’ve ever written.
Anyway, the function got mangled in formatting for the web. This is what it should be…
Create A Remote File Share
This topic came up recently in the PowerShell forum at ScriptingAnswers.com and I thought you might find it helpful. The issue was creating a file share remotely. In the “olden days”, this would have meant using a command line tool like RMTSHARE, which is really just fine. But if you have a larger workflow, doing it natively in PowerShell may be more desirable.
Let me show you how easy it is to create a file share using Windows Management Instrumentation (WMI). I wrote a function called New-FileShare.
Parse HOSTS file with PowerShell
In a recent discussion in the PowerShell forum at ScriptingAnswers.com, a member was trying to parse the HOSTS file on a number of desktops. This is a thankless but probably useful task that seems perfectly suited to a scripted solution. What we came up with was a way to turn entries in the HOSTS file to objects in PowerShell. Once we have an object then we can do all sorts of fun things with it. Here is an enhanced version of the original solution.