Set default application for .ps1 files as powershell.exe [duplicate] - windows

I am distributing a PowerShell script to my team. The script is to fetch an IP address from the Vsphere client, make an mstsc connection, and log it in a shared file.
The moment they used the script they got to know the IP address of machine. After that, they always tend to use mstsc directly instead of running the PowerShell script.
(As they are using mstsc I am not able to know whether they are using the VM frequently or not.)
Mainly they are telling me that running PowerShell is not straightforward.
I am sick by their laziness.
Is there a way to make a PowerShell script work by double clicking a .ps1 file?

Create a shortcut with something like this as the "Target":
powershell.exe -command "& 'C:\A path with spaces\MyScript.ps1' -MyArguments blah"

Or if you want all PS1 files to work the way VBS files do, you can edit the registry like this:
HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\open\command
Edit the Default value to be something like so...
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -noLogo -ExecutionPolicy unrestricted -file "%1"
Then you can just double click all your .PS1 files like you would like to. in my humble opinion, be able to out of the box.
I'm going to call this "The Powershell De-castration Hack". LOL enjoy!

This worked for me on Windows 10 and powershell 5.1:
right click on the .ps1 file
Open with...
Choose another app
Copy the location of powershell.exe to the address bar (by default it won't show windows folder) i.e. C:\Windows\System32\WindowsPowerShell\v1.0
select powershell.exe
select "Always use this app to open .ps1 files"
click OK

Be aware that one of PowerShell's security features is that users can NOT launch script with a double click. Use great care if you modify this setting. An alternative might be to package your script. Some editors like PrimalScript can do that. The users still need PowerShell installed but then they can double-click the exe. And it sounds like your team needs a little education.

I agree that setting a system setting may be a bit much, but the shortcut requiring a hardcoded path is not ideal. A bat file actually solves the problem nicely
RunMyPowershellScript.bat
start powershell -command "& '.\MyPowershellScript.ps1' -MyArguments blah"
This batch file can now be double clicked on, shortcuts can be easily created to the batch file, and the script can be deployed to any folder.

I wrote this a few years ago (run it with administrator rights):
<#
.SYNOPSIS
Change the registry key in order that double-clicking on a file with .PS1 extension
start its execution with PowerShell.
.DESCRIPTION
This operation bring (partly) .PS1 files to the level of .VBS as far as execution
through Explorer.exe is concern.
This operation is not advised by Microsoft.
.NOTES
File Name : ModifyExplorer.ps1
Author : J.P. Blanc - jean-paul_blanc#silogix-fr.com
Prerequisite: PowerShell V2 on Vista and later versions.
Copyright 2010 - Jean Paul Blanc/Silogix
.LINK
Script posted on:
http://www.silogix.fr
.EXAMPLE
PS C:\silogix> Set-PowAsDefault -On
Call Powershell for .PS1 files.
Done!
.EXAMPLE
PS C:\silogix> Set-PowAsDefault
Tries to go back
Done!
#>
function Set-PowAsDefault
{
[CmdletBinding()]
Param
(
[Parameter(mandatory=$false, ValueFromPipeline=$false)]
[Alias("Active")]
[switch]
[bool]$On
)
begin
{
if ($On.IsPresent)
{
Write-Host "Call PowerShell for .PS1 files."
}
else
{
Write-Host "Try to go back."
}
}
Process
{
# Text Menu
[string]$TexteMenu = "Go inside PowerShell"
# Text of the program to create
[string] $TexteCommande = "%systemroot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command ""&'%1'"""
# Key to create
[String] $clefAModifier = "HKLM:\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell\Open\Command"
try
{
$oldCmdKey = $null
$oldCmdKey = Get-Item $clefAModifier -ErrorAction SilentlyContinue
$oldCmdValue = $oldCmdKey.getvalue("")
if ($oldCmdValue -ne $null)
{
if ($On.IsPresent)
{
$slxOldValue = $null
$slxOldValue = Get-ItemProperty $clefAModifier -Name "slxOldValue" -ErrorAction SilentlyContinue
if ($slxOldValue -eq $null)
{
New-ItemProperty $clefAModifier -Name "slxOldValue" -Value $oldCmdValue -PropertyType "String" | Out-Null
New-ItemProperty $clefAModifier -Name "(default)" -Value $TexteCommande -PropertyType "ExpandString" | Out-Null
Write-Host "Done !"
}
else
{
Write-Host "Already done!"
}
}
else
{
$slxOldValue = $null
$slxOldValue = Get-ItemProperty $clefAModifier -Name "slxOldValue" -ErrorAction SilentlyContinue
if ($slxOldValue -ne $null)
{
New-ItemProperty $clefAModifier -Name "(default)" -Value $slxOldValue."slxOldValue" -PropertyType "String" | Out-Null
Remove-ItemProperty $clefAModifier -Name "slxOldValue"
Write-Host "Done!"
}
else
{
Write-Host "No former value!"
}
}
}
}
catch
{
$_.exception.message
}
}
end {}
}

You'll need to tweak registry.
First, configure a PSDrive for HKEY_CLASSES_ROOT since this isn’t set up by default. The command for this is:
New-PSDrive HKCR Registry HKEY_CLASSES_ROOT
Now you can navigate and edit registry keys and values in HKEY_CLASSES_ROOT just like you would in the regular HKCU and HKLM PSDrives.
To configure double-clicking to launch PowerShell scripts directly:
Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 0
To configure double-clicking to open PowerShell scripts in the PowerShell ISE:
Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 'Edit'
To restore the default value (sets double-click to open PowerShell scripts in Notepad):
Set-ItemProperty HKCR:\Microsoft.PowerShellScript.1\Shell '(Default)' 'Open'

Simple PowerShell commands to set this in the registry;
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
Set-ItemProperty -Path "HKCR:\Microsoft.PowerShellScript.1\Shell\open\command" -name '(Default)' -Value '"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -noLogo -ExecutionPolicy unrestricted -file "%1"'

You may set the default file association of ps1 files to be powershell.exe which will allow you to execute a powershell script by double clicking on it.
In Windows 10,
Right click on a ps1 file
Click Open with
Click Choose another app
In the popup window, select More apps
Scroll to the bottom and select Look for another app on this PC.
Browse to and select C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.
List item
That will change the file association and ps1 files will execute by double-clicking them. You may change it back to its default behavior by setting notepad.exe to the default app.
Source

I tried the top-most answers to this question, but encountered error messages. Then I found the answer here:
PowerShell says "execution of scripts is disabled on this system."
What worked well for me was to use this solution:
powershell -ExecutionPolicy Bypass -File script.ps1
You can paste that into a .bat file and double-click on it.

put a simple .cmd file in my subfolder with my .ps1 file with the same name, so, for example, a script named "foobar" would have "foobar.ps1" and "foobar.cmd". So to run the .ps1, all I have to do is click the .cmd file from explorer or run the .cmd from a command prompt. I use the same base name because the .cmd file will automatically look for the .ps1 using its own name.
::====================================================================
:: Powershell script launcher
::=====================================================================
:MAIN
#echo off
for /f "tokens=*" %%p in ("%~p0") do set SCRIPT_PATH=%%p
pushd "%SCRIPT_PATH%"
powershell.exe -sta -c "& {.\%~n0.ps1 %*}"
popd
set SCRIPT_PATH=
pause
The pushd/popd allows you to launch the .cmd file from a command prompt without having to change to the specific directory where the scripts are located. It will change to the script directory then when complete go back to the original directory.
You can also take the pause off if you want the command window to disappear when the script finishes.
If my .ps1 script has parameters, I prompt for them with GUI prompts using .NET Forms, but also make the scripts flexible enough to accept parameters if I want to pass them instead. This way I can just double-click it from Explorer and not have to know the details of the parameters since it will ask me for what I need, with list boxes or other forms.

Navigate REGEDIT to
HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1\Shell
On the right pane, double-click "(Default)"
Delete existing value of "Open" (which launches Notepad) and type "0" (being zero, which launches Powershell directly).
Revert the value if you wish to use Notepad as the default again.

A solution in the same spirit as UNIX shar (shell archive).
You can put your powershell script in a file with the .cmd extension (instead of .ps1), and put this at the start:
#echo off
Rem Make powershell read this file, skip a number of lines, and execute it.
Rem This works around .ps1 bad file association as non executables.
PowerShell -Command "Get-Content '%~dpnx0' | Select-Object -Skip 5 | Out-String | Invoke-Expression"
goto :eof
# Start of PowerShell script here

If you are familiar with advanced Windows administration, then you can use this ADM package (instructions are included on that page) and allow running PowerShell scripts after double click via this template and Local GPO. After this you can simply change default program associated to .ps1 filetype to powershell.exe (use search, it's quite stashed) and you're ready to run PowerShell scripts with double click.
Otherwise, I would recommend to stick with other suggestions as you can mess up the whole system with these administrations tools.
I think that the default settings are too strict. If someone manages to put some malicious code on your computer then he/she is also able to bypass this restriction (wrap it into .cmd file or .exe, or trick with shortcut) and all that it in the end accomplishes is just to prevent you from easy way of running the script you've written.

there is my solution 2022
Install "PowerShell-7.2.2-win-x64.msi"
Right click on file.ps1 and change to exec with "pwsh"
Powershell registry hacks and policy bypass never worked for me.

This is based on KoZm0kNoT's answer. I modified it to work across drives.
#echo off
pushd "%~d0"
pushd "%~dp0"
powershell.exe -sta -c "& {.\%~n0.ps1 %*}"
popd
popd
The two pushd/popds are necessary in case the user's cwd is on a different drive. Without the outer set, the cwd on the drive with the script will get lost.

This is what I use to have scrips run as admin by default:
Powershell.exe -Command "& {Start-Process PowerShell.exe -Verb RunAs -ArgumentList '-File """%1"""'}"
You'll need to paste that into regedit as the default value for:
HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command
Or here's a script that will do it for you:
$hive = [Microsoft.Win32.RegistryKey]::OpenBaseKey('ClassesRoot', 'Default')
$key = $hive.CreateSubKey('Microsoft.PowerShellScript.1\Shell\Open\Command')
$key.SetValue($null, 'Powershell.exe -Command "& {Start-Process PowerShell.exe -Verb RunAs -ArgumentList ''-File """%1"""''}"')

I used this (need to run it only once); also make sure you have rights to execute:
from PowerShell with elevated rights:
Set-ExecutionPolicy=RemoteSigned
then from a bat file:
-----------------------------------------
ftype Microsoft.PowerShellScript.1="C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe" -noexit ^&'%%1'
assoc .ps1=Microsoft.PowerShellScript.1
-----------------------------------------
auto exit: remove -noexit
and voila; double-clicking a *.ps1 will execute it.

In Windows 10 you might also want to delete Windows Explorer's override for file extension association:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1\UserChoice
in addition to the HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\open\command change mentioned in other answers.
See https://stackoverflow.com/a/2697804/1360907

You may not want to but an easy way is just to create a .BAT file and put your command in:
powershell ./generate-strings-table-en.ps1
powershell ./generate-conjoined-tables-it.ps1
Then double-click said BAT file.

You can use the Windows 'SendTo' functionality to make running PS1 scripts easier. Using this method you can right click on
a PS1 script and execute. This is doesn't exactly answer the OP question but it is close. Hopefully, this is useful to others. BTW.. this is helpful for
a variety of other tasks.
Locate / Search for Powershell.exe
Right click on Powershell.exe and choose Open File Location
Right click on Powershell.exe and choose Create Shortcut. Temporarily save some place like your desktop
You might want to open as Admin by default. Select Shortcut > Properties > Advanced > Open As Admin
Open the Sendto folder. Start > Run > Shell:Sendto
Move the Powershell.exe shortcut to the Sendto folder
You should now be able to right click on a PS1 script.
Right Click on a PS1 file, Select the SendTo context option > Select the Powershell shortcut
Your PS1 script should execute.

From http://www.howtogeek.com/204166/how-to-configure-windows-to-work-with-powershell-scripts-more-easily:
Set the default value for the HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell to 0

Related

Opening up Windows Terminal with elevated privileges, from within Windows Terminal

There are plenty of questions here which asks how to elevate priviliges from powershell, and almost all of them recommend this command:
Start-Process -Verb RunAs powershell.exe .....
(or pwsh.exe for Powershell Core)
And this works, it opens up a new Powershell window with elevated privileges, after having gone through a UAC block to verify access.
However, while I'm using Powershell, I'm doing it inside Windows Terminal, the new terminal application for Windows 10, and I would like to open a new instance of Windows Terminal with elevated privileges, not just a Powershell window.
So I tried these commands:
Start-Process -Verb RunAs wt
Start-Process -Verb RunAs wt.exe
Start-Process -Verb RunAs (Get-Command wt)
Start-Process -Verb RunAs "C:\Users\lasse\AppData\Local\Microsoft\WindowsApps\wt.exe"
They all fail with:
Start-Process: This command cannot be run due to the error: The file cannot be accessed by the system..
I'm assuming this has something to do with where the executable is located, within my profile, but if I right-click the Windows Terminal icon I have on my task bar and choose to run it as administrator, it opens up just fine. This is what I want to duplicate.
So is there a way for me to modify either the commands I tried above, or change some access setting that would make this work?
For my specific instance, I simply want to make it simpler to pop open an admin terminal, I don't need a way to elevate arbitrary commands, then I will happily use the commands I have already shown here.
Currently you cannot open an elevated wt.exe session from the command line without workarounds. Workarounds include using gsudo, Using Task Scheduler (I tested this one and it works but you need to use the full path to wt.exe and you can skip the shortcut creation step) OR if you are ok with a keyboard shortcut, the simplest way; using a keyboard shortcut to run Windows Terminal as Admin from the taskbar.
For your use case:
For my specific instance, I simply want to make it simpler to pop open
an admin terminal, I don't need a way to elevate arbitrary commands,
then I will happily use the commands I have already shown here.
The simplest approach will work:
Pin Windows Terminal as the first item on the task bar. Then hit Win+Ctrl+Shift+1 to open it as admin.
If you really must launch Windows Terminal from the command line from within Windows Terminal then create a task in the Task Scheduler:
Give the task a name, check 'Run with highest privileges'.
Choose the 'Actions' tab, click 'New', select 'Start a program' as the action. Put the full path to wt.exe in the 'Program/script field'. Click OK. Click OK again.
Click 'Conditions' tab, uncheck "Start the task only if the computer is on AC power".
Click 'Settings' tab, make sure "Allow task to be run on demand" is checked and uncheck "Stop the task if running for longer than".
Finally in your shell (Powershell), launch an elevated Windows Terminal session by running the command: schtasks /run /TN "TASK_NAME" where TASK_NAME is the name you gave the task in step 1.
Try this:
powershell "Start-Process -Verb RunAs cmd.exe '/c start wt.exe'"
Also check out these links:
WT.exe command line arguments:
https://learn.microsoft.com/en-us/windows/terminal/command-line-arguments?tabs=windows
Article about adding Open Windows Terminal Command Prompt to the context menu in Explorer (includes Admin):
https://dkcool.tailnet.net/2020/07/add-open-windows-terminal-command-prompt-to-the-explorer-context-menu-in-windows-10/
Article about adding Open Admin Command Prompt to the context menu in Explorer:
https://dkcool.tailnet.net/2019/05/add-open-admin-command-prompt-to-the-explorer-context-menu-in-windows-10/
Not a direct answer but another option if you have PowerToys is to:
Alt + Space, type Terminal,
Select Run as Administrator (or Ctrl + Shift + Enter)
You can install PowerToys using WinGet
With recent releases, this issue appears to be fixed. It works now, doing exactly as you originally tried and failed (Start-Process -verb RunAs wt). I would recommend trying again now with the latest releases (at least Windows Terminal, and perhaps PowerShell as well).
No need for workarounds anymore!!
Windows Terminal has a feature to automatically run as administrator in the preview; no need for workarounds now.
Pin to the Taskbar and hold Ctrl + Shift while left clicking on the Windows Terminal icon.
In my particular case I also need Windows Terminal opened as administrator all the time. This is what I did, run "where wt" to display the path where Windows Terminal application exe is located, it should be C:\Users\YOURUSER\AppData\Local\Microsoft\WindowsApps\wt.exe.
I created a shortcut to that file and checked "Run as administrator" in the advanced properties, then I just pinned it to start and voila. You can delete your temporary shortcut after that if you want.
You can create a shortcut to always run Windows Terminal as administrator using this powershell script:
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Desktop\Windows Terminal.lnk")
$Shortcut.TargetPath = "$env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.WindowsTerminal_8wekyb3d8bbwe\wt.exe"
$Shortcut.Save()
$bytes = [System.IO.File]::ReadAllBytes("$Home\Desktop\Windows Terminal.lnk")
$bytes[0x15] = $bytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
[System.IO.File]::WriteAllBytes("$Home\Desktop\Windows Terminal.lnk", $bytes)
You can just paste it and run it from Windows Powershell ISE, it will create a Windows Terminal.lnk file on your desktop. Whenever you double click on that shortcut Windows terminal will run as an admnnistrator
I know this answer does not fully match your question but given that also other answers were oriented in this way I hope this won't disturb the discussion.
I always need to run PowerShell as Administrator and I only want to use Windows Terminal, which given it's restrictions cannot be configured to run always as Administrator.
I hated the need to use shortcuts and other hacks I found being suggested online, so I think I found a better solution but you have to pay the cost of a 1/2 seconds at startup.
Locate your user profile (A profile is a Windows PowerShell ISE script that runs automatically when you start a new session) using _ $PROFILE
Edit profile with any preferred editor _ code $PROFILE
Adde the following code to the profile file and save it
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
$host.ui.rawui.windowtitle="Bootstrapper"
Start-Process -Verb RunAs wt
} else {
$Processes = Get-Process | Where-Object {$_.mainWindowTitle -eq 'Bootstrapper' -and $_.name -eq 'WindowsTerminal'}
if($Processes.count -gt 0)
{
Stop-Process -Id $Processes[0].id
}
}
What the script do?
You can pin Windows Terminal icon to your application bar and when you click there WT will start as non elevated user, but the profile will understand if this is the case.
When you are not running as Administrator it will change the name of the window and start a new WT as administrator.
The new instance will also execute the profile file and if the instance is runinng as Administrator, it will look for the WT named Bootstrapper and kill it.
This proces takes between one and two seconds, I prefer this way other than right clicking on the icon.
It's likely you were just facing a Path issue. I know that the command examples you gave (e.g. Start-Process -verb RunAs wt) have worked for me for some time (as mentioned in #fialdrexs's answer).
Did you install Windows Terminal from a Github release or from the Store?
I currently have the following entry in my settings.json profiles list to add an elevated Windows Terminal to the drop down options:
{
// https://github.com/microsoft/terminal/issues/632#issuecomment-663686412
"name": "Windows Terminal (elevated)",
"commandline": "%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe -Command Start-Process -Verb RunAs \"shell:appsFolder\\Microsoft.WindowsTerminal_8wekyb3d8bbwe!App\"",
"hidden": false,
"icon": "ms-appx:///Images/Square44x44Logo.targetsize-32.png"
}
The comment with the GitHub link should get you to where I originally found this information.
I find a away to workaround, just create a file bat with content
powershell Start-Process -Verb RunAs wt.exe
Save file yourfile.bat to folder you want.
Add path folder to System Environment.
Press WINDOW + R and type file bat name.
Currently this problem was fixed, but it ended up with a weird issue. Running wt.exe from Win+R, searching it on start menu, and starting itself from the terminal, show the same error message.
The application was unable to start correctly (0xc0000022). Click OK
to close the application.
However it works when executing wt via Command Prompt, PowerShell, and PowerShell Core.
So just a quick workaround answer, start PowerShell and run the command,
Start-Process -Verb RunAs wt.exe;
or the simpler
start -verb runas wt

How can I get PowerShell current location every time I open terminal from file explorer

I can open PowerShell window in any directory using Windows File Explorer.
I want to run a script every time a new PowerShell window is open and use current directory where it was open in the script.
Using $profile let me for automatic script execution but $pwd variable does not have directory used to open PowerShell window but has C:\WINDOWS\system32. I understand PowerShell starts in C:\WINDOWS\system32, run $profile and next change location used with File Explorer. How can I get file explorer current directory it when my script is executes from $profile or maybe there is another way to automatic execute my script after PowerShell window is open?
Note: The answer below provides a solution based on the preinstalled File Explorer shortcut-menu commands for Window PowerShell.
If modifying these commands - which requires taking ownership of the registry keys with administrative privileges - or creating custom commands is an option, you can remove the NoWorkingDirectory value from the following registry keys (or custom copies thereof):
HKEY_CLASSES_ROOT\Directory\shell\Powershell
HKEY_CLASSES_ROOT\Directory\Background\shell\Powershell
Doing so will make the originating folder the working directory before PowerShell is invoked, so that $PROFILE already sees that working directory, as also happens when you submit powershell.exe via File Explorer's address bar.[1]
Shadowfax provides an important pointer in a comment on the question:
When you hold down Shift and then invoke the Open PowerShell window here shortcut-menu command on a folder or in the window background in File Explorer, powershell.exe is initially started with C:\Windows\System32 as the working directory[1], but is then instructed to change to the originating folder with a Set-Location command passed as a parameter; e.g., a specific command may look like this:
"PowerShell.exe" -noexit -command Set-Location -literalPath 'C:\Users\jdoe'
As an aside: The way this shortcut-menu command is defined is flawed, because it won't work with folder paths that happen to contain ' chars.
At the time of loading $PROFILE, C:\Windows\System32 is still effect, because any command passed to -command isn't processed until after the profiles have been loaded.
If you do need to know in $PROFILE what the working directory will be once the session is open, use the following workaround:
$workingDir = [Environment]::GetCommandLineArgs()[-1] -replace "'"
[Environment]::GetCommandLineArgs() returns the invoking command line as an array of arguments (tokens), so [-1] returns the last argument, assumed to be the working-directory path; -replace "'" removes the enclosing '...' from the result.
However, so as to make your $PROFILE file detect the (ultimately) effective working directory (location) irrespective of how PowerShell was invoked, more work is needed.
The following is a reasonably robust approach, but note that a fully robust solution would be much more complex:
# See if Set-Location was passed and extract the
# -LiteralPath or (possibly implied) -Path argument.
$workingDir = if ([Environment]::CommandLine -match '\b(set-location|cd|chdir\sl)\s+(-(literalpath|lp|path|PSPath)\s+)?(?<path>(?:\\").+?(?:\\")|"""[^"]+|''[^'']+|[^ ]+)') {
$Matches.path -replace '^(\\"|"""|'')' -replace '\\"$'
} else { # No Set-Location command passed, use the current dir.
$PWD.ProviderPath
}
The complexity of the solution comes from a number of factors:
Set-Location has multiple aliases.
The path may be passed positionally, with -Path or with -LiteralPath or its alias -PSPath.
Different quoting styles may be used (\"...\", """...""", '...'), or the path may be unquoted.
The command may still fail:
If the startup command uses prefix abbreviations of parameter names, such as -lit for -LiteralPath.
If a named parameter other than the path follows set-location (e.g., -PassThru).
If the string set-location is embedded in what PowerShell ultimately parses as a string literal rather than a command.
If the startup command is passed as a Base64-encoded string via -EncodedCommand.
[1] When you type powershell.exe into File Explorer's address bar instead, the currently open folder is made the working directory before PowerShell is started, and no startup command to change the working directory is passed; in that case, $PROFILE already sees the (ultimately) effective working directory.
1.open the registry (command:regedit)
2.find out the path \HKEY_CLASSES_ROOT\Directory\Background\shell\Powershell\command (not \HKEY_CLASSES_ROOT\Directory\Background\shell\cmd\command)
3.the default value should be powershell.exe -noexit -command Set-Location -literalPath "%V"
4.you can change some param,
ps: you change the command to cmd.exe /s /k pushd "%V". if you do so, shift & right button in the explorer will open the cmd, not powershell

Are there any fundamental incompatibilities when using a CMD script in a console using PowerShell?

I have an extensive set of CMD scripts for an automation suite.
In a console using CMD.exe, everything works fine. After installing the Windows Creator's update, where PowerShell becomes the new default Windows shell via Explorer's menu options, my scripts break at-random. I can't provide any meaningful code for repro for two main reasons:
No obvious error is occurring; my automated scripts just hang, eventually
The halt isn't even occurring in the same place each time
What I can tell you is that the suite heavily relies on exit codes, use of findstr.exe, and type.
I know things like Windows macros, e.g., %Var% are not compatible, but I was assuming that since the only call I did was to a .bat file, .bat behavior would be the only thing I would need to worry about.
If that's not the case, should my initial .bat be triggering the direct execution of a CMD.exe instance with my parameters? If so, what's the best way to do that, PowerShell-friendly?
eryksun's comments on the question are all worth heeding.
This section of the answer provides a generic answer to the generic question in the question's title. See the next section for considerations specific to the OP's scenario.
Generally speaking, there are only a few things to watch out for when invoking a batch file from PowerShell:
Always include the specific filename extension (.bat or .cmd) in the filename, e.g., script_name.bat
This ensures that no other forms of the same command (named script_name, in the example) with higher precedence are accidentally executed, which could be:
In case of a command name without a path component:
An alias, function, cmdlet, or an external executable / PowerShell script (*.ps1) that happens to be located in a directory listed earlier in the $env:PATH (%PATH%) variable; if multiple executables by the same name are in the same (earliest) directory, the next point applies too.
In case of a command name with a path component:
A PowerShell script (*.ps1) or executable with the same filename root whose extension comes before .bat or .cmd in the %PATHEXT% environment variable.
If the batch file is located in the current directory, you must prefix its filename with ./
By design, as a security measure, PowerShell - unlike cmd.exe - does NOT invoke executables located in the current directory by filename only, so invoking script_name.bat to invoke a batch file of that name in the current directory does not work.[1]
Instead, you must use a path to target such an executable so as to explicitly signal the intent to execute something located in the current directory, and the simplest approach is to use prefix ./ (.\, if running on Windows only); e.g., ./script_name.bat.
When passing parameters to the batch file:
Either: be aware of PowerShell's parsing rules, which are applied before the arguments are passed to the batch file - see this answer of mine.
Or: use --% (the PSv3+ stop-parsing symbol) to pass the remaining arguments as if they'd been passed from a batch file (no interpretation by PowerShell other than expansion of %<var>%-style environment-variable references).
[1] eryksun points out that on Vista+ you can make cmd behave like PowerShell by defining environment variable NoDefaultCurrentDirectoryInExePath (its specific value doesn't matter).
While ill-advised, you can still force both environments to always find executables in the current directory by explicitly adding . to the %PATH% / $env:PATH variable; if you prepend ., you get the default cmd behavior.
As for your specific scenario:
After installing the Windows Creator's update, where PowerShell becomes the new default Windows shell via Explorer's menu options
This applies to the following scenarios:
Pressing Win-X (system-wide keyboard shortcut) now offers PowerShell rather than cmd in the shortcut menu that pops up.
Using File Explore's File menu now shows Open Windows PowerShell in place of Open command prompt (cmd).
However, nothing has changed with respect to how batch files are invoked when they are opened / double-clicked from File Explorer: The subkeys of HKEY_CLASSES_ROOT\batchfile and HKEY_CLASSES_ROOT\cmdfile in the registry still define the shell\open verb as "%1" %*, which should invoke a batch file implicitly with cmd /c, as before.
However, per your comments, your batch file is never run directly from File Explorer, because it require parameter values, and it is invoked in two fundamental ways:
Explicitly via a cmd console, after entering cmd in the Run dialog that is presented after pressing Win-R (system-wide keyboard shortcut).
In this case, everything should work as before: you're invoking your batch file from cmd.
Explicitly via PowerShell, using File Explorer's File menu.
Per your comments, the PowerShell console may either be opened:
directly in the directory in which the target batch file resides.
in an ancestral directory, such as the root of a thumb drive on which the batch file resides.
In both cases, PowerShell's potential interpretation of arguments does come into play.
Additionally, in the 2nd case (ancestral directory), the invocation will only work the same if the batch file either does not depend on the current directory or explicitly sets the current directory (such as setting it to its own location with cd /d "%~dp0").
This is a non-answer solution if encountering the question's specific behavior. I've verified all my halting scripts stopped halting after implementing a shim-like workaround.
As erykson said, there doesn't appear to be a reason why using a shim would be required. The goal is then to explicitly launch the script in CMD when using PowerShell, which aligns with Jeff Zeitlin's original suggestion in the question's comments.
So, let's say you're in my shoes with your own script_name.bat.
script_name.bat was your old script that initializes and kicks off everything. We can make sure that whatever was in script_name.bat is correctly run via CMD instead of PowerShell by doing the following:
Rename script_name.bat to script_name_shim.bat
Create a new script_name.bat in the same directory
Set its contents to:
#echo off
CMD.exe /C "%~dp0script_name_shim.bat" %*
exit /b %errorlevel%
That will launch your script with CMD.exe regardless of the fact that you started in PowerShell, and it will also use all your command-line arguments too.
This looks like a chicken egg problem, wihtout knowing the code it's difficult to tell where the problem is.
There are a ton of ways to start batches with cmd.exe even in win10cu.
Aliases are only a problem when working interactively with the PowerShell console and expecting behavior as it used to be in cmd.exe.
The aliases depend also on the loaded/imported modules and profiles.
This small PowerShell script will get all items from Help.exe and
perform a Get-Command with the item.
internal commands without counterparts in PoSh are filtered out by the ErrorAction SilentlyContinue.
Applications (*.exe files) are assumed identical and removed by the where clause.
help.exe |
Select-String '^[A-Z][^ ]+'|
ForEach-Object {
Get-Command $_.Matches.Value -ErrorAction SilentlyContinue
}| Where-Object CommandType -ne 'Application'|Select *|
Format-Table -auto CommandType,Name,DisplayName,ResolvedCommand,Module
Sample output on my system, all these items will likely work differently in PowerShell:
CommandType Name DisplayName ResolvedCommand Module
----------- ---- ----------- --------------- ------
Alias call call -> Invoke-Method Invoke-Method pscx
Alias cd cd -> Set-LocationEx Set-LocationEx Pscx.CD
Alias chdir chdir -> Set-Location Set-Location
Alias cls cls -> Clear-Host Clear-Host
Alias copy copy -> Copy-Item Copy-Item
Alias del del -> Remove-Item Remove-Item
Alias dir dir -> Get-ChildItem Get-ChildItem
Alias echo echo -> Write-Output Write-Output
Alias erase erase -> Remove-Item Remove-Item
Alias fc fc -> Format-Custom Format-Custom
Function help pscx
Alias md md -> mkdir mkdir
Function mkdir
Function more
Alias move move -> Move-Item Move-Item
Function Pause
Alias popd popd -> Pop-Location Pop-Location
Function prompt
Alias pushd pushd -> Push-Location Push-Location
Alias rd rd -> Remove-Item Remove-Item
Alias ren ren -> Rename-Item Rename-Item
Alias rmdir rmdir -> Remove-Item Remove-Item
Alias set set -> Set-Variable Set-Variable
Alias sc sc -> Set-Content Set-Content
Alias sort sort -> Sort-Object Sort-Object
Alias start start -> Start-Process Start-Process
Alias type type -> Get-Content Get-Content

Trouble creating multiple folders from a TXT file in Windows PowerShell or CMD?

I was wondering if there is a way I could create multiple folders from a TXT document in windows PowerShell or CMD? I have a TXT file full of drawing numbers, like 5614-E-1459_SH 1 (except theirs about 500 hundred of them). Due to policies at my job I am not aloud to use third party software, so I was wondering if there was a way to do this from the command prompt or Windows PowerShell? I know that mkdir "C:\temp\5614-E-1459_SH 1" will create one of the folders I need. But is there a way to extract the files from a TXT and make it output into folders, without third party software like Text2Folders?
I have gotten this far with the PowerShell script, but as I dont have admin writes at work (where its needed most) I get the Set-ExecutionPolicy error. Is there a work around this?
$Users = Get-Content "C:\Users\usermgx\Desktop\folderDir.txt"
ForEach ($user in $users)
{
$newPath = Join-Path "C:\Users\usermgx\Desktop\Dir" -childpath $user
New-Item $newPath -type directory
}
First, you need to update your execution policy so that you can run scripts. You can do it permanently from an administrative PowerShell prompt by running:
Set-ExecutionPolicty RemoteSigned -Scope LocalMachine
If you don't have administrative rights, you can set the execution policy when you call the powershell.exe executable. From CMD:
powershell.exe -ExecutionPolicy RemoteSigned -Command C:\Path\to\your\script.ps1
Finally, you can run your script from the PowerShell ISE. Just open a new untitled document, enter your code, and hit F5, which will execute the code in script pane. I don't believe this is blocked by the execution policy.
Get-Content "C:\Users\usermgx\Desktop\folderDir.txt" |
ForEach-Object {
$dirPath = Join-Path "C:\Users\usermgx\Desktop\Dir" $_
New-Item $dirPath -ItemType Directory
}
Fortunately for what you are trying to do this is pretty easy to do with a CMD script and you won;t have to muck with the execution policy:
#echo off
for /F %%u in (C:\Users\usermgx\Desktop\folderDir.txt) DO (
mkdir "C:\Users\usermgx\Desktop\Dir\"%%u
)
If you want your powershell version to work you must chenge the execution policy as you've noted. But without admin access, you'll have to limit the scope to just yourself, like this:
set-executionpolicy -scope CurrentUser -ExecutionPolicy RemoteSigned
This is the same as #zdan but handles certain extra features like long path and filenames and spaces etc in the new foldernames.
#echo off
for /F "delims=" %%a in ('type "C:\Users\usermgx\Desktop\folderDir.txt" ') DO (
mkdir "C:\Users\usermgx\Desktop\Dir\%%a"
)
The "workaround" for the "`Set-ExecutionPolicy error'" is to set the execution policy to allow scripts to run. See http://technet.microsoft.com/en-us/library/ee176961.aspx . It's set to the most restrictive by default but any admin worth his salt will set it to something less restrictive based on what the environment requires.
Once you've done that, your script looks solid.

How to run a PowerShell script

How do I run a PowerShell script?
I have a script named myscript.ps1
I have all the necessary frameworks installed
I set that execution policy thing
I have followed the instructions on this MSDN help page
and am trying to run it like so:
powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1' (with or without --noexit)
which returns exactly nothing, except that the file name is output.
No error, no message, nothing. Oh, when I add -noexit, the same thing happens, but I remain within PowerShell and have to exit manually.
The .ps1 file is supposed to run a program and return the error level dependent on that program's output. But I'm quite sure I'm not even getting there yet.
What am I doing wrong?
Prerequisites:
You need to be able to run PowerShell as an administrator
You need to set your PowerShell execution policy to a permissive value or be able to bypass it
Steps:
Launch Windows PowerShell as an Administrator, and wait for the PS> prompt to appear
Navigate within PowerShell to the directory where the script lives:
PS> cd C:\my_path\yada_yada\ (enter)
Execute the script:
PS> .\run_import_script.ps1 (enter)
Or: you can run the PowerShell script from the Command Prompt (cmd.exe) like this:
powershell -noexit "& ""C:\my_path\yada_yada\run_import_script.ps1""" (enter)
according to Invoking a PowerShell script from cmd.exe (or Start | Run) by Kirk Munro.
Or you could even run your PowerShell script asynchronously from your C# application.
If you are on PowerShell 2.0, use PowerShell.exe's -File parameter to invoke a script from another environment, like cmd.exe. For example:
Powershell.exe -File C:\my_path\yada_yada\run_import_script.ps1
If you want to run a script without modifying the default script execution policy, you can use the bypass switch when launching Windows PowerShell.
powershell [-noexit] -executionpolicy bypass -File <Filename>
Type:
powershell -executionpolicy bypass -File .\Test.ps1
NOTE: Here Test.ps1 is the PowerShell script.
I've had the same problem, and I tried and tried... Finally I used:
powershell.exe -noexit "& 'c:\Data\ScheduledScripts\ShutdownVM.ps1'"
And put this line in a batch-file, and this works.
If you only have PowerShell 1.0, this seems to do the trick well enough:
powershell -command - < c:\mypath\myscript.ps1
It pipes the script file to the PowerShell command line.
Pretty easy. Right click the .ps1 file in Windows and in the shell menu click on Run with PowerShell.
Open PowerShell in administrator mode
Run: set-executionpolicy unrestricted
Open a regular PowerShell window and run your script.
I found this solution following the link that was given as part of the error message: About Execution Policies
Make sure to run set-ExecutionPolicy default once you're done, or you will be exposed to security risks.
Using cmd (BAT) file:
#echo off
color 1F
echo.
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "PrepareEnvironment.ps1"
:EOF
echo Waiting seconds
timeout /t 10 /nobreak > NUL
If you need run as administrator:
Make a shortcut pointed to the command prompt (I named it
Administrative Command Prompt)
Open the shortcut's properties and go to the Compatibility tab
Under the Privilege Level section, make sure the checkbox next to "Run this program as an administrator" is checked
An easy way is to use PowerShell ISE, open script, run and invoke your script, function...
In case you want to run a PowerShell script with Windows Task Scheduler, please follow the steps below:
Create a task
Set Program/Script to Powershell.exe
Set Arguments to -File "C:\xxx.ps1"
It's from another answer, How do I execute a PowerShell script automatically using Windows task scheduler?.
If your script is named with the .ps1 extension and you're in a PowerShell window, you just run ./myscript.ps1 (assuming the file is in your working directory).
This is true for me anyway on Windows 10 with PowerShell version 5.1 anyway, and I don't think I've done anything to make it possible.
Give the path of the script, that is, path setting by cmd:
$> . c:\program file\prog.ps1
Run the entry point function of PowerShell:
For example, $> add or entry_func or main
You can run from cmd like this:
type "script_path" | powershell.exe -c -
Use the -File parameter in front of the filename. The quotes make PowerShell think it is a string of commands.
I've just found the method what Microsoft do when we right click on a ps1 script and click on "Run with PowerShell" :
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C:\Users\USERNAME\Desktop\MYSCRIPT.ps1'"
With the appropriate execution policy, you should just be able to call the file directly and Windows will associate it with PowerShell
C:\my_path\yada_yada\run_import_script.ps1
That does not do so well with arguments. The real answer to your question is that you are missing the & to say "execute this"
powershell.exe '& C:\my_path\yada_yada\run_import_script.ps1'

Resources