Alan's Blog

"Yeah. I wrote a script that will do that."

Open the PowerShell ISE (and other Programs) with Alternate Credentials

Posted on March 14th, 2017

RunAS for PowerShell is pretty easy. This opens the ISE:


Filed under PowerShell, Scriptlets | No Comments »

Export to HTML with Style Sheet Header

Posted on February 25th, 2017


This a modification of something I picked up on the web.  It allows you to export to an HTML file with a centered table and centered title.  Any number of properties in the data are made into auto-sized columns.


Tags: ,
Filed under PowerShell, Scripting, Scriptlets | No Comments »

A Dot Source Reminder for Advanced Functions

Posted on February 25th, 2017

One of the problems with writing advanced functions is that new PowerShell users think that they don’t do anything.  Frankly, I couldn’t figure out a way to get a notification to work, so I reached out to the sponsor for the Charlotte PowerShell User Group, Microsoft PFE Brian Wilhite. Brian sent me some code  which I incorporated into the following snippet:

If you expect to have the end-user save the function with code calling it at the bottom of the script, try this version which won’t prompt if there is added text at the bottom:

Put either bit of code at the bottom of any advanced function. If the script runs inside the ISE, you will get something like this:

If you run it inside the PowerShell console you will see:

If you use the shell menu option to Run with PowerShell, it adds a pause so the script does not close.  Note that the colors are not rendered properly in the example text  — the first line has green text, following lines display with default colors.

Update:  if the code above doesn’t pause when you use “Run with PowerShell” from the shell context menu, replace if (([Environment]::GetCommandLineArgs()) -match '&'){pause} with if ([regex]::IsMatch([Environment]::GetCommandLineArgs(), 'powershell_ise') -eq $false){Pause}. For more on this see this post.

Tags: , , ,
Filed under PowerShell, Scripting, Scriptlets | No Comments »

Fix User’s Home Directory Permissions with Take Ownership

Posted on February 15th, 2017

Fix-HomeDrivePerms.ps1 is a PowerShell script which attempts to reset folder security when the permissions are really hosed.  It uses a take ownership function, Set-Owner, by Boe Prox, instead of takeown.exe, but does shell out to iCacls.exe.  I wrote this to fix home directories where a user might be logged on with files open a the time, and so it doesn’t rip out the old permissions and replace.

This will require a some editing to run, and this code fragment is set up to do one user folder at a time.  But it might get you going in the right direction.

Script Text

Tags: ,
Filed under PowerShell, Scripting, Scriptlets, Security, Windows Administration | No Comments »

Consolidating Data For Report: Getting from Group-Object to CSV

Posted on February 4th, 2017

I am working on some scripts which show AD domain controllers by their site.  My goal was to have the data combined so that  each site has a single row, with the server name and IP address for each site being shown joined by semi-colons.






None of the examples for doing this worked for me. But this did:

Tags: , , , , ,
Filed under PowerShell, Scripting, Scriptlets | No Comments »

Get the NetBIOS AD Domain Name from the FQDN

Posted on January 30th, 2017

I hate using NameTranslate, because it is a COM object, and because the output is often really hard to get into a clean, trimmed string.  The netBIOS name isn’t a part of the AD domain object, but I suspected that the information could be gotten using a LDAP query.  My searching lead me to a post on StackFlow.  It wasn’t PowerShell, but it did give me an interesting hint.  The filter’s objectcategory was “CrossRef”.  I used this to port the code to PowerShell:

This query is quick, and avoids the formatting problems with NameTranslate.  There is a large table of LDAP queries on TechNet, but this one isn’t in the list.

Tags: , ,
Filed under Active Directory, Functions, PowerShell, Scriptlets, Windows Administration | No Comments »

Function to Get Splat Parameters from a CSV File

Posted on October 1st, 2016

Splatting is the use of a hash table to pass parameters to a PowerShell script.  As PowerShell scripts grow in complexity, you may want to pass a large number of parameters multiple times, for scripts which spin up a lab environment, or for Desired State Configuration.  It took me a while to figure out how to read a CSV file and pass the data out for splatted parameters.  Export-CSVtoSplats.ps1 is an function that does this, with an example of usage in the comments.

This is not as elegant as the solution proposed by Jaap Brasser, but I couldn’t manage to get his to work with my test function.
Script Text

Tags: , ,
Filed under PowerShell, Scripting, Scriptlets | No Comments »

CIM_DATETIME Conversion Functions

Posted on September 5th, 2016

Also known as WMI Time, or WBEM DateTime,  CIM_DATETime,, is that odd Windows DateTime format that shows values looking like “20160905103517.816236-240”  The COM Object that presents this is  WbemScripting.SWbemDateTime, and you frequently see code to convert to this format using it, or a tortuous series of string manipulations.  My rule of thumb is this:  If you can avoid using a COM object, you should.  Here are two  functions to handle these dates using .NET.

The second function converts from the CIM DateTime string back to an ordinary date object:

An example of the output:

Filed under PowerShell, Scripting, Scriptlets, WMI | No Comments »

Suggesting a default filename

Posted on January 3rd, 2014

I like to leave log files where people can find them.  Typically this is on their desktop, or in the same folder where the script ran from.  I wrote the following bit of code which creates a date based report file and offers it as a default.  It is pretty straightforward, but I did not know about the date formatting bit until I did this.

#Define default log
$lfd ="$env:userProfileDesktop"+$(Get-Date).ToString("yyyyMMdd_HHmm")+"_Report.txt"

#prompt for logfile unless specfied with -Logfile parameter (not shown)
if (!$LogFile){
    $logfile = Read-Host "Enter path to text log file, or ENTER for default,`n$lfd"

#use default log if not specified
if ($LogFile.Length -eq 0) {$LogFile = $lfd}

Write-Host $LogFile

Filed under PowerShell, Scriptlets | No Comments »

Is this string an IP?

Posted on November 30th, 2013

PowerShell one liner to test whether a string is a valid IP address:

 Function IsIP ($strTest){[Net.IPAddress]::TryParse($strTest,[ref]$null)}

This works for both IPv4 and IPv6.


Filed under PowerShell, Scripting, Scriptlets | No Comments »

Please Note

All the scripts are saved as .txt files. Newer files have a "View Script" button which will let you save or open a script in notepad. For earlier posts, the easiest way to download with IE is to right click on the link and use "Save Target As". Rename file from Name_ext.txt to Name.ext.

To see a full post after searching, please click on the title.

PowerShell Scripts were written with Version 3 or 4.

https connections are supported.

All new users accounts must be approved, as are comments. Please be patient. It is pretty easy to figure out my email address from the scripts, and you are welcome to contact me that way.

Site Search



SQL Site

Bad Behavior has blocked 169 access attempts in the last 7 days.