Alan's Blog

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

PowerShell Get Column Names for a CSV File

Posted on August 13th, 2017

Get-Member doesn’t always show you what is under the hood for an object.  For that you need the .PSObject property.  Here PSObject.Properties contains CSV column names

Tags:
Filed under PowerShell, Scriptlets | No Comments »

Powershell Date LDAP filters

Posted on August 13th, 2017

This snippet can be used for easier date formatting when using an LDAP date filter with PowerShell.  This demonstrates how to get users created within the previous 30 days using LDAP:

 

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

PowerShell Pause with Progress Bar

Posted on August 13th, 2017

This snippet of PowerShell was written to have show users something more interesting than “Sleeping for 15 seconds” in a script.  Notice that I splat the progress parameters.

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

Fix Creation Date Later than Date Modified with PowerShell

Posted on August 13th, 2017

A weird and annoying thing happened to my home directory at work when it was moved from Windows to a storage appliance. The file CreationTime was lost on all the files and was set to the date of the data move. Particularly annoying was seeing the CreationTime being more recent than the LastWriteTime attribute. At one point in my IT career I thought that these attributes could not be changed by the user. I was wrong. In .NET these methods are available using System.IO. Here is the script — you will need to edit some choices a the top to use it.

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

OU of Current PC from anywhere in the Forest

Posted on August 12th, 2017

There are a lot of ways to get the OU of the current computer, but most don’t work if you are outside your home domain. This code does, without requiring AD cmdlets:

Tags: ,
Filed under Active Directory, Scripting, Scriptlets | No Comments »

An Empty Pipe Element is not Allowed Here – Workaround

Posted on August 12th, 2017

This code gives you the error, “An Empty Pipe Element is not Allowed Here”:

The workaround which solves this problem is to make it an array by enclosing the code in @(), Example:

Tags:
Filed under PowerShell, Scriptlets | No Comments »

Split a List and Remove Empty Lines with PowerShell

Posted on August 12th, 2017

I often have lists where I have to split the list, and remove empty lines.  This is how I do it:

This method uses a regular expression with three different variations of line break, then the .NET method of removing empty lines.

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

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:

 

Tags:
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 »

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

Categories

Archives

SQL Site

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