Windows

How to Properly Pause a PowerShell Script

When I first learned about PowerShell, I immediately wanted to convert my batch files into PowerShell scripts and then enhance them with additional features. When I started that process, I quickly learned that PowerShell didn’t have anything equivalent to cmd.exe’s “Pause” command. I looked for a solution online and found the Microsoft TechNet article Windows PowerShell Tip: Press Any Key to Continue. This is the solution recommended by the article:

Write-Host "Press any key to continue ..."

$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

However, the above solution has two things wrong with it:

  • It doesn’t work from within Windows PowerShell ISE
  • Unlike cmd.exe’s “Pause” command, pressing keys like Ctrl and Alt causes the script to continue

To fix these two issues, I decided to write my own “Pause” function:

Function Pause ($Message = "Press any key to continue . . . ") {
	If ($psISE) {
		# The "ReadKey" functionality is not supported in Windows PowerShell ISE.

		$Shell = New-Object -ComObject "WScript.Shell"
		$Button = $Shell.Popup("Click OK to continue.", 0, "Script Paused", 0)

		Return
	}

	Write-Host -NoNewline $Message

	$Ignore =
		16,  # Shift (left or right)
		17,  # Ctrl (left or right)
		18,  # Alt (left or right)
		20,  # Caps lock
		91,  # Windows key (left)
		92,  # Windows key (right)
		93,  # Menu key
		144, # Num lock
		145, # Scroll lock
		166, # Back
		167, # Forward
		168, # Refresh
		169, # Stop
		170, # Search
		171, # Favorites
		172, # Start/Home
		173, # Mute
		174, # Volume Down
		175, # Volume Up
		176, # Next Track
		177, # Previous Track
		178, # Stop Media
		179, # Play
		180, # Mail
		181, # Select Media
		182, # Application 1
		183  # Application 2

	While ($KeyInfo.VirtualKeyCode -Eq $Null -Or $Ignore -Contains $KeyInfo.VirtualKeyCode) {
		$KeyInfo = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
	}

	Write-Host
}

Here’s the minified version:

Function Pause($M="Press any key to continue . . . "){If($psISE){$S=New-Object -ComObject "WScript.Shell";$B=$S.Popup("Click OK to continue.",0,"Script Paused",0);Return};Write-Host -NoNewline $M;$I=16,17,18,20,91,92,93,144,145,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183;While($K.VirtualKeyCode -Eq $Null -Or $I -Contains $K.VirtualKeyCode){$K=$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")};Write-Host}
Advertisements

Install PowerShell 2.0 on Windows XP Using a Batch File

I wrote a batch file (for use with Windows XP only) that installs PowerShell 2.0. Here are the three things you need to know about it:

  1. The batch file installs PowerShell 2.0 only if it is not already installed
  2. If service pack 3 for Windows is not installed, the batch file exits
  3. If .NET Framework 2.0 (at least service pack 1) is not installed, the batch file automatically installs the x86 version of .NET Framework 2.0 service pack 1

Here are the two files used by the batch file:

  1. Microsoft .NET Framework 2.0 Service Pack 1 (x86)
  2. Update for Windows XP (KB968930): includes Windows PowerShell 2.0 and Windows Remote Management (WinRM) 2.0

Here is the code for the batch file:

@ECHO OFF

REM Make sure this batch file is being run with Windows XP
VER | FINDSTR /L "5.1." > NUL
IF %ERRORLEVEL% NEQ 0 ECHO It appears that you're not using Windows XP, so this batch file will exit now.&GOTO EOF

REM See if PowerShell is installed
FOR /F "tokens=3" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\PowerShell\1" /v Install ^| FIND "Install"') DO SET PowerShellInstalled=%%A
CLS

IF NOT "%PowerShellInstalled%"=="0x1" ECHO PowerShell doesn't appear to be installed.&GOTO CheckPrerequisites

REM PowerShell is installed, so now see which version it is
FOR /F "tokens=3" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine" /v PowerShellVersion ^| FIND "PowerShellVersion"') DO SET PowerShellVersion=%%A
CLS

IF "%PowerShellVersion%"=="" (
 ECHO PowerShell appears to be installed, but the version number was unable to be
 ECHO determined.
 GOTO CheckPrerequisites
)

ECHO PowerShell %PowerShellVersion% appears to be installed.
IF %PowerShellVersion%==2.0 GOTO EOF

:CheckPrerequisites
ECHO.
ECHO Version 2 will now be installed.
ECHO.

REM Make sure service pack 3 for Windows is installed
REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CSDVersion | FIND "Service Pack 3" > NUL
IF %ERRORLEVEL% EQU 0 GOTO CheckNETFramework2SP
CLS

ECHO It appears that you're using Windows XP, but without service pack 3. Please
ECHO install service pack 3 and then run this batch file again.
ECHO.
GOTO EOF

:CheckNETFramework2SP
REM Service pack 3 for Windows is installed, so now make sure .NET Framework 2.0 (at least SP1) is installed
FOR /F "tokens=3" %%A IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" /v SP ^| FIND "SP"') DO SET NETFramework2SP=%%A
CLS

IF NOT "%NETFramework2SP%"=="" IF NOT "%NETFramework2SP%"=="0x0" GOTO InstallPowerShell2

ECHO Installing .NET Framework 2.0 SP1...
START "" /WAIT NetFx20SP1_x86.exe /q /norestart
ECHO.

:InstallPowerShell2
ECHO Installing PowerShell 2.0...
START "" /WAIT WindowsXP-KB968930-x86-ENG.exe /quiet /passive /norestart

:EOF

An Easy Way to Run PowerShell Scripts

According to the Microsoft TechNet article Running Windows PowerShell Scripts, these are the three ways to run a PowerShell script:

  1. From within PowerShell
  2. From a shortcut
  3. From the “Run” dialogue box

It turns out that, as of PowerShell 2.0, there is another way, which is to simply right-click the PowerShell script and choose “Run with PowerShell”. But what if you have several PowerShell scripts that you want to run in succession and not simultaneously? I ran into this problem, so I came up with a solution that involves a simple batch file. With this batch file, all you have to do to run one or more PowerShell scripts is drag it onto the batch file’s icon and let go. Here is the code for the batch file:

@ECHO OFF
CLS
IF NOT EXIST "%windir%\System32\WindowsPowerShell\v1.0\powershell.exe" GOTO NoPowerShell

IF NOT "%~1"=="" GOTO Start

ECHO Drag a PowerShell script (or multiple PowerShell scripts) onto this batch
ECHO file's icon and let go to run it.
ECHO.
PAUSE
GOTO EOF

:Start
REM Make sure the "%~1" parameter is a valid path
IF "%~1"=="" GOTO EOF
IF NOT EXIST "%~1" GOTO ShiftParameters

ECHO This batch file will now launch the following file using PowerShell:
ECHO.
ECHO %~1
ECHO.
ECHO *** BEGIN ***
"%windir%\System32\WindowsPowerShell\v1.0\powershell.exe" ^&\"%~1\"

REM If an error occurs, pause the batch file so that the user can review the error message
IF %ERRORLEVEL% EQU 1 PAUSE

ECHO *** END ***
ECHO.

:ShiftParameters
SHIFT
GOTO Start

:NoPowerShell
ECHO PowerShell wasn't found at the following location:
ECHO.
ECHO "%windir%\System32\WindowsPowerShell\v1.0\powershell.exe"
ECHO.
ECHO Please install PowerShell and try again.
ECHO.
PAUSE

:EOF

How to Manage Your Finances for Free on Your PC

Managing your finances on your computer can be extremely helpful. For example, if you write a check to somebody and they don’t deposit it until a few weeks later, you may forget you even wrote the check. Also, debit card transactions can sometimes take a few days to appear on your online bank statement. Even when they do appear, if the transaction is at the stage where it hasn’t cleared yet, the amount might not be accurate. This is often the case with transactions at restaurants (usually due to the tip) and gas stations. These scenarios can all lead to NSF fees. Paying fees to a bank because you weren’t responsible with your money is the same as flushing your money down the toilet, and nobody wants that!

What if you want to track where your money is going? Or what if you’re trying to plan ahead and want to know how much your electric bill was for a certain month last year so that you’ll have a rough idea of how much it’ll be for that same month this year?

You can see why managing your finances on your computer is a good idea. There are so many reasons to do it, and with a free application like Money Manager Ex (comparable to Quicken or Microsoft Money), there’s no reason not to.

Here is a tutorial video I made on how to use Money Manager Ex:

Use a Batch File to Detect Windows 2K, XP, 2003, Vista, or 7

A while ago I was in a situation where I needed to be able to detect the version of Windows that was being used to execute a batch file. After searching the Internet, the best script I found was this one by Rod of Rod.Net. I made some modifications to it and I came up with what I believe is the best script to use to detect which version of Windows is being used as long as it’s 2K, XP, 2003, Vista, or 7. Here’s the script:

@ECHO OFF
SET OSVersion=Unknown

VER | FINDSTR /L "5.0" > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=2000

VER | FINDSTR /L "5.1." > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=XP

VER | FINDSTR /L "5.2." > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=2003

VER | FINDSTR /L "6.0." > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=Vista

VER | FINDSTR /L "6.1." > NUL
IF %ERRORLEVEL% EQU 0 SET OSVersion=7

IF %OSVersion%==Unknown (
 ECHO Unable to determine your version of Windows.
) ELSE (
 ECHO You appear to be using Windows %OSVersion%
)

ECHO.
PAUSE

How to Manage Home Pages in Internet Explorer

Although I’m not a big fan of Internet Explorer (as made clear in my last blog post), there are times when you have no choice but to use it. This tutorial will show you how to manage home pages in Internet Explorer versions 7 and 8. If you’re not sure which version you have, open Internet Explorer (if you’re not using it already) and visit http://www.thismachine.info/. It will say which version you’re using in the “What browser am I using?” section.

Say Hello to the Home Page Button

Internet Explorer has a home page button in the toolbar, located here:

Internet Explorer home page button

Internet Explorer home page button

Notice in the picture above that the home page button has two parts to it—the part with the house icon and the part with the arrow. If you click the part with the house icon, Internet Explorer will open your home page(s). If you click the part with the arrow, you can manage your home pages. On my computer, this is what I see when I click the arrow:

Internet Explorer home page button (clicked)

Internet Explorer home page button (clicked)

I only have one home page (Google), but you can have several home pages if you want to. Each home page will be opened in their own tab. More information on tabs will be provided later in this tutorial.

How to Replace Your Home Page or Add a New One

  1. Go to the website of your choice
  2. Click the arrow directly to the right of the house icon (shown above)
  3. Click “Add or Change Home Page…”
  4. You will be asked if you want the website you’re at to be your only home page or if you want it to be added to your home page tabs. The choice is yours.

How Does Having Multiple Home Pages Work?

If you have multiple home pages and you open Internet Explorer, each home page will be opened in its own tab in Internet Explorer. Tabs are shown at the top of the Internet Explorer window, like this:

Internet Explorer tabs

Internet Explorer tabs

This is a very useful feature because if you always go to a few particular websites every time you open Internet Explorer, this saves you the trouble of having to go to each website one at a time. You can easily switch between the websites by clicking the tabs at the top of the Internet Explorer window.

What’s Wrong with Internet Explorer?

Internet Explorer is the most widely-used web browser in the world. Most people use it simply because it’s the only web browser they have on their computer. They aren’t aware that they have other choices such as Mozilla Firefox, Opera, Safari, and Google Chrome. So when they want to check their e-mail, or get driving directions, or watch a video, or go on Facebook, what do they do? They fire up Internet Explorer and away they go. So what’s the problem? Well, there are actually three problems:

  1. Internet Explorer is insecure and unsafe.
  2. Internet Explorer is much slower than other web browsers, as shown by the graphs on this page.
  3. With Internet Explorer, things often don’t look the way they should.

Although the first two points in the above list are important, this article focuses on the third point. Here is a link to a page on my personal website showing ten examples of things that look wrong in Internet Explorer, but look right in Mozilla Firefox, Opera, Safari, and Google Chrome:

Ten Things That Look Wrong in Internet Explorer

Please note that there are far, far more problems than just ten. The point of the page is to make it easy for the average person to see the problems. For a much more technical and exhaustive list of problems in Internet Explorer, see this page.

Why is Internet Explorer so different from other web browsers?

For whatever reason, Microsoft (the maker of Internet Explorer) decided to program Internet Explorer in such a way that it breaks many important rules of the web set by the World Wide Web Consortium (W3C).

What is Microsoft doing to fix Internet Explorer?

As of March 2009, Microsoft is still developing the next version of Internet Explorer (version 8), which aims to be more compliant with the rules of the web. They have a public preview available to download, but it’s still far from perfect. Even when it’s finished, I’m sure it will still have many flaws.

What should I do?

You could cross your fingers and hope that Internet Explorer version 8 ends up being a good web browser, but I highly doubt that will happen. I recommend immediately switching to Mozilla Firefox, Opera, Safari, or Google Chrome. That way you can surf the web much more securely, much more quickly, and with the confidence that you’re seeing the websites the way that they were intended to be seen.

Be aware that there are some websites that require Internet Explorer. If you happen to run into one of those websites, go ahead and use Internet Explorer for that website, but don’t use it for anything else.