How to Run PowerShell Code You Copied: Paste, .ps1, or .cmd

Advertisement
en-cps-eyecatch.jpg PowerShell
Advertisement

Most posts on this blog come with a PowerShell script you can copy. If you have never used PowerShell, though, the next step is not obvious. Where do you paste it? What does saving it as a .ps1 file actually mean, and why does nothing happen when you double-click it?

Here are the three ways I run those scripts, tested on Windows 11 with PowerShell 5.1 and 7. To run a script once, paste it into PowerShell. To keep it, save it as a .ps1 file in Notepad. To start it with a double-click, or to hand it to a coworker, put a three-line .cmd file next to it. If the script contains any non-English characters, save it as UTF-8 with BOM, or Windows PowerShell 5.1 will garble them.

The screenshots come from a Japanese copy of Windows, so the menus are in Japanese. The English labels are in the text.

PowerShell window after pasting the practice script. A table of file types with counts is shown, followed by the line Counted 11 files
The practice script, pasted into PowerShell as-is

The basics: PowerShell, .ps1 files, .cmd files

PowerShell is the command-line app built into Windows. It lets you do things like list files, give them new names in bulk, or export data to CSV by typing commands. It needs no install and no admin rights, so it works even on a locked-down work PC. Windows 10 and 11 ship with Windows PowerShell 5.1. The newer PowerShell 7 is a separate install. The scripts on this blog are written to work in both.

A .ps1 file is a saved PowerShell script. A .cmd file (the same thing as a .bat file) is a batch file for the old Command Prompt. Both are plain text, so Notepad is all you need.

Method Best for Execution policy
1. Paste into PowerShell Running a script once, trying it out Doesn't apply
2. Save as .ps1, right-click to run Running the same script again later Applies. It may block the script
3. Double-click a .cmd next to it Regular use, sharing with coworkers Set for that run only, without changing the PC

The execution policy is the PowerShell setting that decides whether .ps1 files may run. On a fresh Windows 11 PC it often blocks them, and method 2 then fails with running scripts is disabled on this system. Commands you paste in are not blocked.

Copy the code from the post

Hover over a code box, and a few buttons appear in its top-right corner. The one with two overlapping squares (Copy to clipboard) copies the whole box. It is more reliable than dragging, which tends to miss the first or last line.

Buttons that appear in the top-right corner of a code box on this blog, with the copy button highlighted
The copy button on a code box

This post uses the practice script below. It counts the files in a folder by extension and changes nothing. Line 2 sets the folder. As written, it counts your Downloads folder.

# Practice script: count the files in a folder by type (extension)
$folder = "$env:USERPROFILE\Downloads"   # <- change this to the folder you want to check

$files = Get-ChildItem -LiteralPath $folder -File -Recurse -ErrorAction SilentlyContinue
$files | Group-Object Extension | Sort-Object Count -Descending |
    Select-Object @{Name = "Type"; Expression = { $_.Name }}, @{Name = "Files"; Expression = { $_.Count }} |
    Format-Table -AutoSize
Write-Host "Counted $($files.Count) files (PowerShell $($PSVersionTable.PSVersion))"

The other scripts on this blog also keep the values you need to edit near the top. Edit them in Notepad before you paste. It is much easier there than in the PowerShell window.

Method 1: Paste into PowerShell

Right-click the Start button and click Terminal. You can also search the Start menu for PowerShell. You don't need to run it as administrator.

Press Ctrl+V or right-click to paste. Windows Terminal warns you when the text has more than one line. If it is a script you copied yourself, click Paste anyway.

Windows Terminal warning about pasting multiple lines, with two buttons: Paste anyway, Cancel (Japanese UI)
The multi-line paste warning. The highlighted button is Paste anyway

PowerShell then runs the lines one by one, and the result appears as in the first screenshot. Sometimes the last line just sits at the prompt. That happens when the copied text doesn't end with a line break. Press Enter, and it runs.

This method is not affected by the execution policy, so it is a good first test on a work PC. Nothing is saved, though, so next time you copy it from the post again.

Method 2: Save as a .ps1 file in Notepad

Save it with the encoding set to UTF-8 with BOM

Open Notepad, paste the script, and make your edits, such as the folder path.

The practice script pasted into Notepad (Japanese UI)
The script in Notepad. The status bar shows UTF-8

Go to File > Save as. Give it a name ending in .ps1, such as count-files.ps1. Then change Encoding at the bottom to UTF-8 with BOM before you click Save.

Notepad Save as dialog with count-files.ps1 as the file name. The encoding list is open, with the BOM option highlighted at the bottom (Japanese UI)
Name ends in .ps1. Encoding is the last item, UTF-8 with BOM

Notepad defaults to plain UTF-8, without a BOM. For a script that contains only English letters and symbols, either choice works. In my test, the English practice script ran fine both ways in 5.1 and 7. The BOM matters as soon as the file has other characters, such as an accented folder name, a non-English comment, or a curly quote. More on that below.

In the Windows 11 Notepad, the file was saved as count-files.ps1 even with Save as type left at Text Documents (*.txt). No .txt was added. Still, check it in File Explorer. The Type column should say PS1 File. If it says Text Document, the real name ends in .txt.

File Explorer in Details view. count-files.cmd is a Windows Command Script, count-files.ps1 is a PS1 File (Japanese UI)
Check the Type column: PS1 File for .ps1, Windows Command Script for .cmd

This trap is common when you create the file with New > Text Document in File Explorer. If file extensions are hidden, typing count-files.ps1 as the new name actually gives you count-files.ps1.txt. Turn on View > Show > File name extensions to see the full name.

Run it with Run with PowerShell

By default, double-clicking a .ps1 file opens it in Notepad instead of running it. That is on purpose, so a script you only meant to look at doesn't run by accident. To run it, right-click the .ps1 file and click Run with PowerShell. On the Windows 11 menu, it may be under Show more options.

Right-click menu of count-files.ps1 in File Explorer, with Run with PowerShell highlighted (Japanese UI)
Right-click the .ps1 file. The third item is Run with PowerShell

With Run with PowerShell, the window closes the moment the script ends, whether it worked or not. On my PC it was open for about half a second, too short to read anything. To keep it open, add this line at the end of the script and save it again.

Read-Host "Press Enter to close"

If the window still closes instantly after adding that line, the script was stopped before it started. The usual cause is the execution policy. Run the file from a PowerShell window, and you'll see an error saying it cannot be loaded because running scripts is disabled on this system. On your own PC, this one command fixes it for your account, with no admin rights needed. Microsoft explains the options in about_Execution_Policies. On a work PC where you can't change settings, use method 3.

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Method 3: Double-click a .cmd file

If right-clicking every time gets old, put a .cmd file in the same folder as the .ps1. Paste these three lines into Notepad and save it as count-files.cmd. On line 2, use the name of your own .ps1 file.

@echo off
powershell -NoProfile -ExecutionPolicy RemoteSigned -File "%~dp0count-files.ps1"
pause
Part What it does
@echo off Hides the commands themselves
-NoProfile Starts PowerShell without your personal profile
-ExecutionPolicy RemoteSigned Uses RemoteSigned for this run only. The PC setting stays the same
%~dp0 The folder the .cmd file is in, so the pair still works after you move the folder
pause Keeps the window open until you press a key

Double-click the .cmd file. The result appears, followed by Press any key to continue . . ., so you can read it before the window closes. The .cmd file holds only plain English characters, so its encoding doesn't matter. Save the .ps1 file as UTF-8 with BOM, same as in method 2.

Window opened by double-clicking count-files.cmd, showing the file count table with the pause prompt below it (Japanese UI)
Started by double-clicking the .cmd file. It waits for a key at the end

To share a script with coworkers, give them both files in one folder and tell them to double-click the .cmd file. They don't need to touch their execution policy. This doesn't work if your company sets the execution policy through Group Policy. That setting wins over -ExecutionPolicy, so the script won't run. Follow your company's rules and ask IT. This command shows where the policy comes from. A value in MachinePolicy or UserPolicy means Group Policy sets it.

Get-ExecutionPolicy -List

Things that tripped me up

Without a BOM, PowerShell 5.1 garbles non-English characters

Windows PowerShell 5.1 reads a file without a BOM using the system's legacy code page, not UTF-8. A BOM is a short marker at the start of the file that says this is UTF-8. PowerShell 7 assumes UTF-8 either way. I saved the same Japanese script three ways, ran each copy in both versions, and repeated the test with the English practice script.

Saved as PowerShell 5.1 PowerShell 7
UTF-8 (Notepad default) Garbled text, then a parser error Works
UTF-8 with BOM Works Works
ANSI Works Runs, but the text shows as ???
English-only script, UTF-8 Works Works

In 5.1, the garbled characters swallowed a closing quote, so the error was about a missing string terminator, not about encoding. That makes it hard to guess the cause. UTF-8 with BOM was the only choice that worked in both versions.

Parser errors in PowerShell 5.1 after running a script saved as UTF-8 without a BOM. The Japanese text is garbled (Japanese UI)
The Japanese script saved without a BOM, run in 5.1. The real problem is the encoding, but the error points at a missing quote

For a .cmd file with non-English text, use ANSI

For .cmd files, ANSI is the right choice. Command Prompt reads batch files in the legacy code page, so a .cmd file with non-English text should be saved as ANSI. In my test, a Japanese echo line saved as UTF-8 came out garbled, and the ANSI copy displayed correctly. Keeping the .cmd file to plain English characters, as in method 3, avoids the issue.

Summary

  • To run a script once, paste it into PowerShell. The execution policy doesn't apply
  • To keep it, save it as .ps1 in Notepad, and pick UTF-8 with BOM if it has any non-English characters
  • To run it with a double-click or share it, put a three-line .cmd file in the same folder

References

Comments

Copied title and URL