# Powershell script in Notepad works but not as .ps1

**URL:** <https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659>\
**Category:** PowerShell Help\
**Created:** [November 12, 2018, 7:59am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659 "2018-11-12T07:59:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [November 12, 2018, 7:59am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/1 "2018-11-12T07:59:45Z")

</div>

Hi, I have a powershell script, that I created in word, which opens O365 and gets mailbox sizes etc automatically, then closes. To run it, I normally copy the text into powershell straight from Notepad, and it all works as expected. I thought I’d change it to a .ps1 extension, and just pull it directly into powershell, but I’m getting 403 event errors, and the powershell window opens and closes so rapidly I can’t get a handle on the error?

Any ideas?

&nbsp;

thanks

Jason

[pre]

$Path = “F:\Data\2 - Reports\XXXXX”  
$Password = Get-Content $Path\O365Pwd.txt | ConvertTo-SecureString  
$User = Get-Content $Path\O365User.txt  
$Credential = New-Object System.Management.Automation.PsCredential($User,$Password)

Import-Module MsOnline

Connect-MsolService -Credential $Credential  
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri [https://outlook.office365.com/powershell-liveid/](https://outlook.office365.com/powershell-liveid/) -Credential $Credential -Authentication Basic -AllowRedirection

Import-PSSession $Session  
$H = hostname  
$Date = ((Get-Date).ToString(‘dd-MM-yyyy’))

[/pre]

this is part of the code (I have the O365 modules installed, and as stated, if i copy direct from Notepad into powershell works fine)

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [November 12, 2018, 8:01am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/2 "2018-11-12T08:01:55Z")

</div>

sorry typo - I created the original script in Notepad!

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [November 12, 2018, 8:12am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/3 "2018-11-12T08:12:59Z")

</div>

Can you post the error message ?

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [November 12, 2018, 8:28am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/4 "2018-11-12T08:28:48Z")

</div>

I don’t actually get to see the error, the window closes too quickly…but I have the following from EventViewer:

[pre]

Log Name: Windows PowerShell  
Source: PowerShell  
Date: 12/11/2018 13:17:54  
Event ID: 403  
Task Category: Engine Lifecycle  
Level: Information  
Keywords: Classic  
User: N/A  
Computer: Jason-XXXX  
Description:  
Engine state is changed from Available to Stopped.

Details:  
NewEngineState=Stopped  
PreviousEngineState=Available

SequenceNumber=15

HostName=ConsoleHost  
HostVersion=5.1.17134.228  
HostId=152fe65e-9439-433e-a9ab-2d17e5bb7a1c  
HostApplication=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe F:\Data\2 - Reports\XXXX\4 - XXXX O365 Mailbox List Report.ps1  
EngineVersion=5.1.17134.228  
RunspaceId=6f2a43f8-2764-4d49-9158-c31bddc726fc  
PipelineId=  
CommandName=  
CommandType=  
ScriptName=  
CommandPath=  
CommandLine=  
Event Xml:  
\<Event xmlns=“[http://schemas.microsoft.com/win/2004/08/events/event](http://schemas.microsoft.com/win/2004/08/events/event)”\>  
\<System\>  
\<Provider Name=“PowerShell” /\>  
\<EventID Qualifiers=“0”\>403\</EventID\>  
\<Level\>4\</Level\>  
\<Task\>4\</Task\>  
\<Keywords\>0x80000000000000\</Keywords\>  
\<TimeCreated SystemTime=“2018-11-12T13:17:54.887094900Z” /\>  
\<EventRecordID\>3492\</EventRecordID\>  
\<Channel\>Windows PowerShell\</Channel\>  
\<Computer\>JasonXXXXX\</Computer\>  
\<Security /\>  
\</System\>  
\<EventData\>  
\<Data\>Stopped\</Data\>  
\<Data\>Available\</Data\>  
\<Data\> NewEngineState=Stopped  
PreviousEngineState=Available

SequenceNumber=15

HostName=ConsoleHost  
HostVersion=5.1.17134.228  
HostId=152fe65e-9439-433e-a9ab-2d17e5bb7a1c  
HostApplication=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe F:\Data\2 - Reports\XXXX\4 - XXXX O365 Mailbox List Report.ps1  
EngineVersion=5.1.17134.228  
RunspaceId=6f2a43f8-2764-4d49-9158-c31bddc726fc  
PipelineId=  
CommandName=  
CommandType=  
ScriptName=  
CommandPath=  
CommandLine=\</Data\>  
\</EventData\>  
\</Event\>

[/pre]

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [November 12, 2018, 9:13am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/5 "2018-11-12T09:13:07Z")

</div>

Can’t see anything which can close the shell, can you call the script from an existing console or debug it in PowerShell ISE or VSCode.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [November 12, 2018, 9:32am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/6 "2018-11-12T09:32:35Z")

</div>

Try putting pause at the end. Are you running it on another computer? The convertto-securestring won’t work without a key in that case.

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [November 12, 2018, 9:41am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/7 "2018-11-12T09:41:08Z")

</div>

hi, it’s all from the same computer (my laptop) with access to the same pwd files etc. even adding a pause doesn’t stop the windows closing fairly rapidly! so hard to diagnose.

&nbsp;

Thanks for all your help - I’ll try ISE and see if that works!

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [November 12, 2018, 9:44am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/8 "2018-11-12T09:44:07Z")

</div>

running inside ISE - works as expected, it’s just dropping the .ps1 into Powershell standard that’s got the problem…Confuseddotcom!

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [November 12, 2018, 9:48am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/9 "2018-11-12T09:48:54Z")

</div>

Is there any code in this which is copied from some other source ? hidden special chars will give problems.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [November 12, 2018, 9:50am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/10 "2018-11-12T09:50:08Z")

</div>

How exactly are you running the ps1 file? A .lnk shortcut with “powershell -file c:\users\me\script.ps1”?

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [November 12, 2018, 9:52am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/11 "2018-11-12T09:52:49Z")

</div>

Hi Js,

I’ve opened the notepad script, then saved it as a .ps1. After that I just dragged it into Powershell’s shell. I have also right clicked and ‘run with powershell’…both do not run. But if I drag it into ISE, it works as expected!

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [November 12, 2018, 9:57am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/12 "2018-11-12T09:57:59Z")

</div>

can you add **Start-Transcript c:\temp\temp.log** at the very beginning and execute once.

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [November 12, 2018, 10:07am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/13 "2018-11-12T10:07:34Z")

</div>

Thanks Kvprasoon, will try that now!

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [November 12, 2018, 10:09am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/14 "2018-11-12T10:09:33Z")

</div>

Sadly, doesn’t even log an event, just shuts donw poershell - far too quickly for me to grab anything useful!

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [November 12, 2018, 10:45am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/15 "2018-11-12T10:45:39Z")

</div>

What is your execution policy ?

Do a **Get-ExecutionPolicy**

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [November 12, 2018, 10:51am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/16 "2018-11-12T10:51:05Z")

</div>

hi, it’s ‘unrestricted’, as I checked that too, just in case it was ‘restricted’.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [November 12, 2018, 11:15am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/17 "2018-11-12T11:15:51Z")

</div>

> [@](#):
>
> Is there any code in this which is copied from some other source ? hidden special chars will give problems.

Did you check this ?

---

<div class="post-metadata">

**Author:** ![jasonleech](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@jasonleech](https://forums.powershell.org/u/jasonleech)\
**Post date:** [November 12, 2018, 11:19am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/18 "2018-11-12T11:19:57Z")

</div>

Hi,

&nbsp;

No hidden code, this is a script that I created from scratch, and as stated earlier, if I just copy from notepad, works fine, also in ISE. reall head scratching on this…wanted to just drag drop and run!

&nbsp;

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [November 12, 2018, 11:37am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/19 "2018-11-12T11:37:24Z")

</div>

I think you’ll probably find that Notepad usually saves txt files in ANSI encoding, whereas PowerShell will expect UTF8 of some form… this usually isn’t a problem, but it can occasionally cause oddities like this.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [November 12, 2018, 11:49am UTC](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659/20 "2018-11-12T11:49:05Z")

</div>

This will probably get off topic, but I’ve taken an interest in encoding.

Powershell 5.1:  
I believe the default encoding is “ansi” (Windows-1251), definitely not ascii.  
Except out-file (including “\>” and “\>\>”) and tee encode by default in “unicode” (utf16).  
Cannot decode utf8 no bom (“no signature”) (at least special characters)  
Can decode utf8 with bom

Powershell 6:  
Default encoding is utf8 no bom (there’s a way to reliably identify it)

[Next page](https://forums.powershell.org/t/powershell-script-in-notepad-works-but-not-as-ps1/11659.md?page=2)
