unable to run PowerShell script

%3CLINGO-SUB%20id%3D%22lingo-sub-390470%22%20slang%3D%22en-US%22%3Eunable%20to%20run%20PowerShell%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-390470%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20on%20windows%207%20professional%20and%20created%20a%20powershell%20script%20that%20I%20cannot%20run.%20my%20security%20setting%20is%20set%20to%26nbsp%3B%20unrestricted.%20I%20have%20run%20it%203%20ways%20and%20nothing%20happens.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFirst%20I%20right%20click%20the%20script%20and%20choose%20Run%20with%20PowerShell%2C%20cmd%20window%20pops%20up%20stays%20up%20for%20few%20seconds%20and%20closes%20by%20itself%2C%20nothing%20happens.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESecond%20I%20open%20the%20cmd%20and%20use%26nbsp%3B.%5Cencrypt_daily_file.ps1%20command%20and%20run%20it%20from%20the%20directory%20where%20the%20script%20is%20saved%2C%20the%20script%20file%20is%20opens%20in%20notepad%2C%20that's%20it.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThird%20I%20paste%20powershell%20-executionpolicy%20bypass%20-File%20.%5Cencrypt_file2.ps1%20in%20cmd%20window%20and%20the%20commad%20is%20written%20to%20the%20screen%20but%20nothing%20happens%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20my%20script%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24gpgr%20%3D%20'gpg%20--yes%20--recipient%20junk'%3C%2FP%3E%3CP%3E%26lt%3B%23%24filename%20%3D%20'%5C%5Cweb02%5CSFTP%5Cusers%5CFROM%5CEncounter_NPI_'%23%26gt%3B%3CBR%20%2F%3E%24filename%20%3D%20'm%3A%5Cdepression%20sub%20query'%3CBR%20%2F%3E%24date%20%3D%20(Get-Date).ToString('yyyyMMdd')%3CBR%20%2F%3E%24encrypt%20%3D%20'--encrypt%20%22'%20%2B%20%24filename%20%2B%20'.txt%22'%3CBR%20%2F%3E%26lt%3B%23%24output%20%3D%20'--output%20%22'%2B%20'c%3A%5Cdocuments%5CEncounter_NPI_'%20%2B%20%24date%20%2B%20'.txt.gpg%22'%23%26gt%3B%3CBR%20%2F%3E%24output%20%3D%20'--output%20%22'%2B%20'm%3A%5Cdepression%20sub%20query'%20%2B%20%24date%20%2B%20'.txt.gpg%22'%3CBR%20%2F%3E%24gpgr%20%2B%20'%20'%20%2B%20%24output%20%2B%20'%20'%20%2B%20%24encrypt%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-390470%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-394080%22%20slang%3D%22en-US%22%3ERe%3A%20unable%20to%20run%20PowerShell%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-394080%22%20slang%3D%22en-US%22%3EGood%20catch!%20I%20think%20I%20should%20have%20tried%20harder%20to%20decipher%20%22the%20commad%20%5Bsic%5D%20is%20written%20to%20the%20screen%20but%20nothing%20happens%22.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-393448%22%20slang%3D%22en-US%22%3ERe%3A%20unable%20to%20run%20PowerShell%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-393448%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F310149%22%20target%3D%22_blank%22%3E%40iworkonline%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETry%20this.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFrom%3A%3C%2FP%3E%3CP%3E%3CSPAN%3E%24gpgr%20%2B%20'%20'%20%2B%20%24output%20%2B%20'%20'%20%2B%20%24encrypt%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ETo%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3EInvoke-Expression%20%22%24gpgr%20%24output%20%24encrypt%22%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-393067%22%20slang%3D%22en-US%22%3ERe%3A%20unable%20to%20run%20PowerShell%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-393067%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F310149%22%20target%3D%22_blank%22%3E%40iworkonline%3C%2FA%3EHello.%20For%20some%20reason%2C%20you%20didn't%20try%20the%20most%20obvious%20thing.%20Open%20PowerShell%20and%20issue%20the%20following%20command%3A%3C%2FP%3E%3CPRE%3E.%5Cencrypt_daily_file.ps1%3C%2FPRE%3E%3CP%3EIf%20PowerShell%20triggered%20an%20execution%20policy%20error%2C%20try%20this%3A%3C%2FP%3E%3CPRE%3ESet-ExecutionPolicy%20-ExecutionPolicy%20Bypass%20-Scope%20Process%3C%2FPRE%3E%3CP%3EAnd%20then%20run%20the%20script%20again.%3C%2FP%3E%3CP%3EAs%20for%20why%20your%20first%20action%20is%20not%20working%2C%20it%20is%20a%20Windows%207%20bug.%20Microsoft%20implemented%20erroneous%20file%20association%20data%20in%20Registry.%20To%20fix%20that%2C%20you%20have%20to%20open%20notepad%2C%20add%20the%20following%20contents%20and%20save%20the%20resulting%20file%20as%20%221.reg%22%20with%20Unicode%20encoding.%20Then%20run%20it%20and%20have%20it%20merge%20with%20Windows%20Registry.%3C%2FP%3E%3CPRE%3EWindows%20Registry%20Editor%20Version%205.00%0A%0A%5BHKEY_LOCAL_MACHINE%5CSOFTWARE%5CClasses%5CMicrosoft.PowerShellScript.1%5D%0A%22EditFlags%22%3Ddword%3A00020000%0A%22FriendlyTypeName%22%3Dhex(2)%3A40%2C00%2C22%2C00%2C25%2C00%2C73%2C00%2C79%2C00%2C73%2C00%2C74%2C00%2C65%2C00%2C6d%2C%5C%0A%20%2000%2C72%2C00%2C6f%2C00%2C6f%2C00%2C74%2C00%2C25%2C00%2C5c%2C00%2C73%2C00%2C79%2C00%2C73%2C00%2C74%2C00%2C65%2C00%2C6d%2C00%2C%5C%0A%20%2033%2C00%2C32%2C00%2C5c%2C00%2C77%2C00%2C69%2C00%2C6e%2C00%2C64%2C00%2C6f%2C00%2C77%2C00%2C73%2C00%2C70%2C00%2C6f%2C00%2C77%2C%5C%0A%20%2000%2C65%2C00%2C72%2C00%2C73%2C00%2C68%2C00%2C65%2C00%2C6c%2C00%2C6c%2C00%2C5c%2C00%2C76%2C00%2C31%2C00%2C2e%2C00%2C30%2C00%2C%5C%0A%20%205c%2C00%2C70%2C00%2C6f%2C00%2C77%2C00%2C65%2C00%2C72%2C00%2C73%2C00%2C68%2C00%2C65%2C00%2C6c%2C00%2C6c%2C00%2C2e%2C00%2C65%2C%5C%0A%20%2000%2C78%2C00%2C65%2C00%2C22%2C00%2C2c%2C00%2C2d%2C00%2C31%2C00%2C30%2C00%2C33%2C00%2C00%2C00%0A%0A%5BHKEY_LOCAL_MACHINE%5CSOFTWARE%5CClasses%5CMicrosoft.PowerShellScript.1%5CDefaultIcon%5D%0A%40%3D%22%5C%22C%3A%5C%5CWindows%5C%5CSystem32%5C%5CWindowsPowerShell%5C%5Cv1.0%5C%5Cpowershell_ise.exe%5C%22%2C1%22%0A%0A%5BHKEY_LOCAL_MACHINE%5CSOFTWARE%5CClasses%5CMicrosoft.PowerShellScript.1%5CShell%5D%0A%40%3D%22Open%22%0A%0A%5BHKEY_LOCAL_MACHINE%5CSOFTWARE%5CClasses%5CMicrosoft.PowerShellScript.1%5CShell%5C0%5D%0A%22MUIVerb%22%3Dhex(2)%3A40%2C00%2C22%2C00%2C25%2C00%2C73%2C00%2C79%2C00%2C73%2C00%2C74%2C00%2C65%2C00%2C6d%2C00%2C72%2C00%2C%5C%0A%20%206f%2C00%2C6f%2C00%2C74%2C00%2C25%2C00%2C5c%2C00%2C73%2C00%2C79%2C00%2C73%2C00%2C74%2C00%2C65%2C00%2C6d%2C00%2C33%2C00%2C32%2C%5C%0A%20%2000%2C5c%2C00%2C77%2C00%2C69%2C00%2C6e%2C00%2C64%2C00%2C6f%2C00%2C77%2C00%2C73%2C00%2C70%2C00%2C6f%2C00%2C77%2C00%2C65%2C00%2C%5C%0A%20%2072%2C00%2C73%2C00%2C68%2C00%2C65%2C00%2C6c%2C00%2C6c%2C00%2C5c%2C00%2C76%2C00%2C31%2C00%2C2e%2C00%2C30%2C00%2C5c%2C00%2C70%2C%5C%0A%20%2000%2C6f%2C00%2C77%2C00%2C65%2C00%2C72%2C00%2C73%2C00%2C68%2C00%2C65%2C00%2C6c%2C00%2C6c%2C00%2C2e%2C00%2C65%2C00%2C78%2C00%2C%5C%0A%20%2065%2C00%2C20%2C00%2C22%2C00%2C2c%2C00%2C2d%2C00%2C31%2C00%2C30%2C00%2C38%2C00%2C00%2C00%0A%0A%5BHKEY_LOCAL_MACHINE%5CSOFTWARE%5CClasses%5CMicrosoft.PowerShellScript.1%5CShell%5C0%5CCommand%5D%0A%40%3D%22%5C%22C%3A%5C%5CWindows%5C%5CSystem32%5C%5CWindowsPowerShell%5C%5Cv1.0%5C%5Cpowershell.exe%5C%22%20%5C%22-Command%5C%22%20%5C%22if((Get-ExecutionPolicy%20)%20-ne%20'AllSigned')%20%7B%20Set-ExecutionPolicy%20-Scope%20Process%20Bypass%20%7D%3B%20%26amp%3B%20'%251'%5C%22%22%0A%0A%5BHKEY_LOCAL_MACHINE%5CSOFTWARE%5CClasses%5CMicrosoft.PowerShellScript.1%5CShell%5CEdit%5D%0A%22NoSmartScreen%22%3D%22%22%0A%0A%5BHKEY_LOCAL_MACHINE%5CSOFTWARE%5CClasses%5CMicrosoft.PowerShellScript.1%5CShell%5CEdit%5CCommand%5D%0A%40%3D%22%5C%22C%3A%5C%5CWindows%5C%5CSystem32%5C%5CWindowsPowerShell%5C%5Cv1.0%5C%5Cpowershell_ise.exe%5C%22%20%5C%22%251%5C%22%22%0A%0A%5BHKEY_LOCAL_MACHINE%5CSOFTWARE%5CClasses%5CMicrosoft.PowerShellScript.1%5CShell%5COpen%5D%0A%0A%5BHKEY_LOCAL_MACHINE%5CSOFTWARE%5CClasses%5CMicrosoft.PowerShellScript.1%5CShell%5COpen%5CCommand%5D%0A%40%3D%22%5C%22C%3A%5C%5CWindows%5C%5CSystem32%5C%5Cnotepad.exe%5C%22%20%5C%22%251%5C%22%22%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Frequent Visitor

I am on windows 7 professional and created a powershell script that I cannot run. my security setting is set to  unrestricted. I have run it 3 ways and nothing happens.

 

First I right click the script and choose Run with PowerShell, cmd window pops up stays up for few seconds and closes by itself, nothing happens.

 

Second I open the cmd and use .\encrypt_daily_file.ps1 command and run it from the directory where the script is saved, the script file is opens in notepad, that's it.

 

Third I paste powershell -executionpolicy bypass -File .\encrypt_file2.ps1 in cmd window and the commad is written to the screen but nothing happens

 

Here is my script

 

$gpgr = 'gpg --yes --recipient junk'

<#$filename = '\\web02\SFTP\users\FROM\Encounter_NPI_'#>
$filename = 'm:\depression sub query'
$date = (Get-Date).ToString('yyyyMMdd')
$encrypt = '--encrypt "' + $filename + '.txt"'
<#$output = '--output "'+ 'c:\documents\Encounter_NPI_' + $date + '.txt.gpg"'#>
$output = '--output "'+ 'm:\depression sub query' + $date + '.txt.gpg"'
$gpgr + ' ' + $output + ' ' + $encrypt

2 Replies
Highlighted

@iworkonlineHello. For some reason, you didn't try the most obvious thing. Open PowerShell and issue the following command:

.\encrypt_daily_file.ps1

If PowerShell triggered an execution policy error, try this:

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

And then run the script again.

As for why your first action is not working, it is a Windows 7 bug. Microsoft implemented erroneous file association data in Registry. To fix that, you have to open notepad, add the following contents and save the resulting file as "1.reg" with Unicode encoding. Then run it and have it merge with Windows Registry.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1]
"EditFlags"=dword:00020000
"FriendlyTypeName"=hex(2):40,00,22,00,25,00,73,00,79,00,73,00,74,00,65,00,6d,\
  00,72,00,6f,00,6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,\
  33,00,32,00,5c,00,77,00,69,00,6e,00,64,00,6f,00,77,00,73,00,70,00,6f,00,77,\
  00,65,00,72,00,73,00,68,00,65,00,6c,00,6c,00,5c,00,76,00,31,00,2e,00,30,00,\
  5c,00,70,00,6f,00,77,00,65,00,72,00,73,00,68,00,65,00,6c,00,6c,00,2e,00,65,\
  00,78,00,65,00,22,00,2c,00,2d,00,31,00,30,00,33,00,00,00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\DefaultIcon]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe\",1"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell]
@="Open"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\0]
"MUIVerb"=hex(2):40,00,22,00,25,00,73,00,79,00,73,00,74,00,65,00,6d,00,72,00,\
  6f,00,6f,00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,\
  00,5c,00,77,00,69,00,6e,00,64,00,6f,00,77,00,73,00,70,00,6f,00,77,00,65,00,\
  72,00,73,00,68,00,65,00,6c,00,6c,00,5c,00,76,00,31,00,2e,00,30,00,5c,00,70,\
  00,6f,00,77,00,65,00,72,00,73,00,68,00,65,00,6c,00,6c,00,2e,00,65,00,78,00,\
  65,00,20,00,22,00,2c,00,2d,00,31,00,30,00,38,00,00,00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\0\Command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" \"-Command\" \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\Edit]
"NoSmartScreen"=""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\Edit\Command]
@="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe\" \"%1\""

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\Open]

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\Open\Command]
@="\"C:\\Windows\\System32\\notepad.exe\" \"%1\""

 

Highlighted

@iworkonline 

 

Try this.

 

From:

$gpgr + ' ' + $output + ' ' + $encrypt

 

To:

Invoke-Expression "$gpgr $output $encrypt"