Windows Powershell ISE:- File Encoding

Background

Last week I was trying to get a Powershell code to work.

As we have different “environments” this particular script needs to work on at least one machine in each distinct environment.

 

Command File

The script creates extracts each time it runs.

I really did not want those extracts to be saved all over the place.

And, so created a network share to directly save the resultant files; or at minimum copy over from locally saved extracts.

To access powershell files on a remote share, we need to pass along extra arguments.

Create Command File

As I did not want to pass those arguments each time, I opted to create a command file.

Codify the arguments in the command file and re-use the file each time I need to run the powershell script.

Windows Powershell ISE

As I was already reviewing the Powershell file using Windows Powershell ISE, I created a new tab within ISE.

Added a line to invoke the powershell script.

 

Command File

Image

Textual

REM Date:- 2022-03-21 07:00 PM
REM Tool:- Windows Powershell ISE
REM Binary Tool:- %windir%\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe
REM Revision History
REM ================
REM     2022-04-01 09:24 AM
REM Invoke Powershell
REM powershell.exe -file \\network-share\resource\script.ps1

 

Execute Command File

Output

Image

Textual

>c:\file.sample\DOSCommandFileCreatedWithWindowsPowershellISE.cmd

>REM Date:- 2022-03-21 07:00 PM
'REM' is not recognized as an internal or external command,
operable program or batch file.

>REM Tool:- Windows Powershell ISE

>

Explanation

Upon trying to run our command files, we see it being spitted back at us.

The lines in question are:-

REM Date:- 2022-03-21 07:00 PM
‘∩╗┐REM’ is not recognized as an internal or external command, operable program or batch file.

TroubleShooting

Outline

  1. Introduction
    • REM are Comments
  2. Encoding
    • Editor
      • Notepad
      • Notepad++

Introduction

REM are Comments

In a DOS Batch file, lines initiated with REM are comments.

So why will comments trip us?

 

Encoding

Editor

Notepad

File Encoding

Let us determine our command script’s file encoding using Notepad.

Step

Here are the steps:-

  1. Open Command File
    • Use Menu To Open File
      • Use Menu Items:- File/Open
  2. Check File Encoding
    • The file’s encoding will be indicated in notepad’s status line
Output – Image

Explanation

The status bar indicates that our file’s encoding is UTF-8.

 

Notepad++

File Encoding

Let us determine our command script’s file encoding using Notepad++.

Step

Here are the steps:-

  1. Open Command File
    • Use Menu To Open File
      • Use Menu Items:- File/Open
  2. Check File Encoding
    • Use File Encoding to get File’s Encoding
      • Use Menu Items:- Encoding
        • The file’s encoding will be the entry highlighted
Output – Image

Explanation

Our command’s file encoding is UTF-8.

 

Remediation

Outline

  1. Encoding
    • Editor
      • Notepad
      • Notepad++

Introduction

Let us go change our command script’s encoding.

Encoding

Editor

Notepad

File Encoding

Using Notepad, let us change the file’s encoding.

Step

Here are the steps:-

  1. Open Command File
    • Use Menu To Open File
      • Use Menu Items:- File/Open
  2. Save As
    • The file’s encoding can be changed by using the menu item File/Save As
    • In the Save As dialog, the file’s encoding can be changed
    • Availed File’s Encodings are shown in the Encoding drop-down
      • ANSI
      • UTF-16 LE
      • UTF-16 BE
      • UTF-8
      • UTF-8 with BOM
Output – Image

Explanation

The status bar indicates that our file’s encoding is UTF-8.

We will change the file’s encoding to ANSI.

 

Notepad++

File Encoding

Let us change our command script’s file encoding using Notepad++.

Step

Here are the steps:-

  1. Open Command File
    • Use Menu To Open File
      • Use Menu Items:- File/Open
  2. Change File Encoding
    • Use File Encoding to get the File’s Current Encoding and Change it, as well
      • Use Menu Items:- Encoding
        • Current
          • The file’s current encoding will be the entry highlighted
        • Supported Encoding
          • Convert to ANSI
          • Convert to UTF-8
          • Convert to UTF-8-BOM
          • Convert to UTF-16 BE BOM
          • Convert to UTF-16 LE BOM
Output – Image

Explanation

In our case, we changed our file’s encoding to ANSI.

 

Summary

In Dev-OPS, please friend a good and capable editor.

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s