Posey's Tips & Tricks

How To Fix a (Seemingly) Corrupt PowerShell Script

A simple four-line script unexpectedly returned a host of errors for Brien. The issue turned out to be deceptively simple.

While writing my previous column, I ran into a really odd problem: I suddenly found that even the simplest of PowerShell scripts no longer worked.

An extremely simple four-line script started spewing error messages that I had never seen before. After a few minutes, though, I saw that the problem and its solution were actually quite simple. Even so, I'd guess that this particular problem affects a lot of people, so I wanted to show you what the issue was and how to fix it.

As I mentioned, I discovered this problem while writing a different column (ironically, it was about how to debug PowerShell scripts). To illustrate a particular point, I created the simple PowerShell script shown below:

Function Do-Something {
$A = '1234'
}
Do-Something

This script doesn't really do anything (which was intentional). The script's last line calls a function called Do-Something that defines a variable named $A. Given the script's simplicity, I was really surprised when I ran it and received an error message. I was even more surprised when I read what the error message said. You can see the error that I got in Figure 1.

[Click on image for larger view.] Figure 1: My simple, four-line script produced this error message.

I couldn't imagine why I had received the error, but I checked my code to make sure that I hadn't accidentally inserted some invalid characters while in the process of saving the file. As expected, though, there was nothing wrong with the code. So what happened?

Initially, the only thing I could think of was that some sort of disk corruption had occurred. Out of curiosity, though, I used the Type command (a leftover from the days of MS-DOS) to display my script's code within PowerShell. That's when I had my second surprise of the night: My PowerShell code was corrupt.

I reloaded the script file into Notepad, intending to fix the problem. However, Notepad displayed the code correctly. That's when I remembered that Microsoft recently made some changes to Notepad.

[Click on image for larger view.] Figure 2: Notepad (on the left) displays my code correctly, while PowerShell displays my code incorrectly.

In all fairness, Notepad was never intended to be used as a PowerShell editor. Even so, it has always been my tool of choice for writing and editing PowerShell scripts. It's a relic from about 30 years ago, so it is super lightweight. I also prefer Notepad's black text on a white background to the color palette used by the PowerShell ISE editor.

As I said, though, Microsoft recently made some updates to Notepad. These changes are subtle but most of them legitimately improve Notepad. Let me show you an example.

Take a look at Figure 3. Notice anything unusual? If you look at the title bar, you will notice an asterisk just to the left of the word "Untitled." The presence of an asterisk indicates that the latest changes to the document have not yet been saved. As I said, the changes that Microsoft has made to Notepad tend to be subtle, but helpful.

Figure 3: Asterisks indicate that the latest changes to the document have not yet been saved.

Back to the topic at hand: Why did Notepad create a corrupt PowerShell script? Check out the Save As screen, shown in Figure 4. There is now an Encoding option shown just to the left of the Save button. PowerShell used to save text documents in ASNI format, but it now defaults to saving documents in UTF-8 format. PowerShell doesn't know how to interpret some of the tokenized UTF-8 characters, thereby giving the illusion that a script is corrupt.

[Click on image for larger view.] Figure 4: There is now an Encoding option shown just to the left of the Save button.

There are two easy ways to fix the problem. The easiest option (and the one I am using) is to simply set the Encoding option back to ANSI. The other option is to use PowerShell ISE or some other text editor that saves documents in ANSI format.

About the Author

Brien Posey is a 19-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.

Featured

  • Microsoft 365 Users Getting My Feed and SharePoint Page Diagnostics

    Microsoft on Monday announced new Microsoft 365 and SharePoint Online improvements that are getting rolled out to subscribers.

  • Using Metadata To Make Non-Text Data Easier To Find

    Content indexing works well for finding files that contain text, but it's no help when searching for non-text data. Brien's workaround is to take advantage of Windows 10's file metadata feature.

  • Microsoft Previews Windows Autopilot for HoloLens 2

    Microsoft on Friday announced a public preview of Windows Autopilot for HoloLens 2, its mixed-reality headset.

  • Microsoft Flirts with Charging for API Software Connections

    Microsoft may have started something new by attempting to charge its customers for software that uses its application programming interfaces (APIs).

comments powered by Disqus