Currently I am trying to make it so I can open up files from PowerShell directly in Sublime Text 3.
I am using this command: Set-Alias subl 'C:\Program Files\Sublime Text 2\sublime_text.exe'
This works for me initially when I type in "subl filename", but whenever I close out of PowerShell and try it again, it will not work unless I re-type the Set-Alias command. Is there any way to permanently make this possible, so I can always type in subl filename ?
Try running Get-Help about_Profiles
As a quick overview:
PowerShell will load the .ps1 file located at the $Profile path into the session when it starts. Adding Set-Alias subl 'C:\Program Files\Sublime Text 2\sublime_text.exe' to that file will set that alias every time a new PowerShell session is opened.
Related
So I've just downloaded Visual Studio Code to use as my default IDE for learning Python. I'm running on a 64-bit machine so I made the default terminal windows powershell.
The place where I'll be saving most of my files is about 8 folders deep which all show up in the terminal before any commands can be written. Is there any way to hide or shorten the file path in the terminal?
As #Biclops suggested, there is good info here: configure PowerShell to only show the current folder in the prompt
However, I needed more basic info to get this to work. This is a very good resource to get started: Windows PowerShell Profiles. So I first followed the steps suggested there:
[always using vscode's integrated terminal using PowerShell]
test-path $profile (is there a profile set up?)
new-item -path $profile -itemtype file -force (assuming the answer to the above is false)
notepad $profile (opens notepad)
paste in (from the SuperUser answer above)
function prompt {
$p = Split-Path -leaf -path (Get-Location)
"$p> "
}
save (you shouldn't have to chose a location, it is already done for you)
reload vscode - you will probably get an error message about running scripts (or just do next step before reload)
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser (at your integrated terminal PS prompt, also from the SuperUser answer)
reload vscode
You should be good to go!
Great Question and Great Answers.
But this information is dated, and Windows is currently updating from Windows 10 to Windows 11. In addition, the base Windows PowerShell has been incorporated into the new Windows Terminal Preview app. which is being used here.
The solution provided above by Mark and Tarruda23 (above) almost works. But Windows throws an error - described below.
The steps:
First, it was necessary to determine whether a profile existed. Using the Windows Explorer, the following path was checked. If a profile already exists, this path shows where an existing profile should be found. On this PC, no profile ( .ps1 ) file existed and this folder was empty. Don't close the Explorer.
C:\Users\prior\OneDrive\Documents\WindowsPowerShell
Since no file exists, a new file needed to be created. This new file must be saved with a specific name - shown below.
Navigate to the empty folder and open PowerShell. The .ps1 profile must be created and saved in this folder. Use the Powershell's build-in text editor to create the new file. Type:
ISE
Then type or paste the following into the empty text file:
function prompt {
$p = Split-Path -leaf -path (Get-Location)
"$p> "
}
Save this file with the following name:
Microsoft.PowerShell_profile.ps1
Use the PowerShell to open Notepad and check that .ps1 file. This demonstrates the Windows system has found the new .ps1. Next close the Notepad.
Notepad $profile
Now the PowerShell is probably displaying an error message in red text. This error message reads in part:
\Microsoft.PowerShell _profile.ps1 cannot be loaded because running scripts is disabled on this system.
Run the PowerShell as the Administrator. Type the following.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
Windows will prompt with a question:
Do you want to change the execution policy?
Type y for yes. This will change and remove the Windows default settings that prevents running script files. Once done, this will remove that error message.
All should be good now. PowerShell will now start and run with shorter and abbreviated PS> prompt that shows either the User name, or the name of the folder where the PowerShell is running.
I have a .ps1 file with my script.
In this script I have a line with:
Start-Process "C:\activatebatch.bat"
If i execute it directly with ps1 file, everything works well. But if I set up a Windows Scheduler with ps1 as executive, the bat file doesn't start. In that bat file I have a WinSCP which sends file to server.
How could I set it to start .bat from .ps1 stored in Windows Scheduler? Or how could I execute WinSCP directly from PowerShell code?
I need it to call for WinSCP to send a file into server - and the options are stored in batch file:
"C:\Program Files (x86)\WinSCP\WinSCP.exe" user:password#IPI.PIP.IP.IP /command ^
"put C:\ss\file.txt /home/scripts/windows/" ^
"exit"
If you have a working WinSCP command line in a batch file, you may need to do few changes to make it compatible with PowerShell:
Batch file use ^ (caret) to escape a new line. PowerShell uses ` (backtick). So replace your carets to backticks.
And you obviously need to escape any characters with a special meaning in PowerShell, particularly $ (dollar sign), ` (backtick) and inner double quotes.
In your simple script, only the first point matters, so the correct command is:
& "C:\Program Files (x86)\WinSCP\WinSCP.exe" user:password#IPI.PIP.IP.IP /command `
"put C:\ss\file.txt /home/scripts/windows/" `
"exit"
Though I'd further recommend you using winscp.com instead of winscp.exe and adding /log switch to enable session logging for debugging purposes.
Also opening a session using a command-line argument is deprecated. You should use open command (and better also specify a protocol prefix – sftp://?).
& "C:\Program Files (x86)\WinSCP\WinSCP.exe" /command `
"open user:password#IPI.PIP.IP.IP" `
"put C:\ss\file.txt /home/scripts/windows/" `
"exit"
WinSCP 5.14 beta can actually generate a PowerShell – WinSCP command template for you.
Though for a better control, it's recommended to use WinSCP .NET assembly from PowerShell.
When you're in file explorer you can click on File > Open Windows Powershell(or its icon in the Quick Access Toolbar) to start an instance of Powershell in the directory that your file explorer is in. I would like to then automatically run a simple command in this directory and close the Powershell window after it is done.
I have tried adding my command to my Powershell Profile but it executes before the path variable has been set and it runs with $pwd being equal to C:\Users\MyUsername (my home directory) or C:\WINDOWS\system32 (seems to be a race condition of some sort, no idea why it does one or the other). To the best of my understanding this is because the file explorer "open in powershell button" opens powershell and THEN cd's to the directory I was in in file explorer. So when the profile.ps1 is ran it is using the only directories it knows if since the cd call hasn't been made yet. This is similar to running the command start powershell.exe in cmd vs start powershell.exe -command "cd 'C:\wherever'". The former correctly runs my profile command while the latter uses the current directory of cmd and not the C:\wherever.
So, obviously the $pwd variable is being assigned at different times in the case of opening it from cmd and opening it from file explorer. Is there some way to delay the execution of a command in the profile until after the shell has fully loaded? Simply sleeping the script doesn't help.
Alternatively, if anyone knows how to edit the registry so that I can change the behavior of clicking File > Open Windows Powershell (since it must have access to some variable storing the current directory and I assume it calls the Powershell executable with this variable as an argument being cd'd to), that would work too.
Then again I could be incredibly naive about how File > Open Windows Powershell and the Powershell instantiation process works.
Any help is greatly appreciated, thank you!
I figured it out in the most hacky, gross way ever, but without easy access to Windows internals this is the only working method I could find. I set up my powershell profile to make my window title my prompt like so:
function Prompt
{
$host.ui.RawUI.WindowTitle = $(get-location)
“PS> “
}
Then I set up a task in the Task Scheduler that was triggered by powershell reaching its prompt (there are 3 possible hooks, when the console is starting up, when it starts an IPC listening thread, and when the console is ready for input). I used the Event Viewer to do this (I was going to post screenshots but I don't have 10 reputation yet).
Then, I set the action on this task to run the script shown below, which reads from the window title of my first instance of powershell
Start-Sleep -s 1
$A = Get-Process -Name powershell | Where-Object -FilterScript {$_.Id -ne $PID}
$B = $A.MainWindowTitle
& C:\Program` Files\MyProgram\MyProgram.exe "$B"
stop-process -Id $A.Id
stop-process -Id $PID
This whole menagerie of events properly runs my program with the current file explorer directory as an argument (and then closes powershell) when I click the little powershell icon on the quick access toolbar in file explorer.
Found a much cleaner and faster way to do this. All I had to do was set up my profile to look like this, no tasks or second instance of powershell required
function Prompt
{
& C:\Program` Files\MyProgram\MyProgram.exe "$pwd"
stop-process -Id $PID
}
Is there a way to open a script file ($profile or otherwise) from within the IDE's command environment (PS> prompt), such that it loads in the IDE itself?
# loads the current user/current host profile in Notepad (probably because .PS1 is associated w/ Notepad.exe)
PS > ii $profile
# same for any random script file
PS > ii .\Foo.ps1
Instead of Invoke-Item, just use ise. When run from the ISE, it will load the file.
PS> ise myscript.ps1
To make Invoke-Item behave like you want (as well as double-clicking from Explorer), you can associate .ps1 files with powershell_ise.exe. Here is a blog post explaining how to do this if you need it.
How can I open up a file in Notepad++ from the Powershell command line?
Inside PowerShell I can simply use the start and get general results
to open a python file with notepad++ here is what I did.
Start notepad++ ex1.py
this will start notepad++ and load the file ex1.py assuming you are in the same directory as the .py file. You can change that by adding the full path name
start notepad++ c:\users\you\desktop\files\ex1.py
Because the default path contains spaces, you have to quote the path to the exe. However because PowerShell is also a scripting language. A string by itself is simply evaluated as a string e.g.:
C:\ PS> 'Hello world'
Hello world
So you have to tell PowerShell you want to invoke the command that is named by the string. For that you use the call operator & e.g.:
C:\ PS> & 'C:\Program Files (x86)\Notepad++\notepad++.exe'
or if notepad++ is in your path:
C:\ PS> notepad++
or if you're in the same dir as the exe:
C:\ PS> .\notepad++
To open Notepad++ with and create a new empty file in the current path
start notepad++ newFile.txt
To open Notepad++ with an existing file
start notepad++ apples.txt
To specify the path and open multiple files
start notepad++ fruits/apples.txt, fruits/oranges.txt, package.json
To extrapolate on the previous answers and tie them up in a tidy bow:
If you want to open a file with spaces in the path or name:
. 'C:\Program Files (x86)\Notepad++\notepad++.exe' 'C:\Temp\File With Spaces.txt'
or
& 'C:\Program Files (x86)\Notepad++\notepad++.exe' 'C:\Temp\File With Spaces.txt'
It can also be set it as an alias:
Set-Alias -Value 'C:\Program Files (x86)\Notepad++\notepad++.exe' -Name 'NotePad'
$FileWithSpaces = 'C:\T e m p\File With Spaces.txt'
NotePad $FileWithSpaces
The top line here can be copied into (one of) your $Profile .ps1 file(s) so you don't need to keep using Set-Alias in every new PS instance.
Edit your profile and add an alias
Set-Alias -name 'npp' -value 'C:\Program Files\Notepad++\notepad++.exe'
Then:
npp c:\temp\test.txt
Edit your profile:
npp $profile
etc
I know this is an old question, but I found a bit of a workaround, quite by accident, and it is extremely straightforward. If you install and maintain Notepad++ via Chocolatey (think apt-get for Windows, but built on top of NuGet), then you get a shim that can be invoked from the command line.
cinst notepad++
And even if you already have an existing installation of Notepad, you can still "install" it from Chocolatey, and it will pull in the existing installation and maintain it.
I use Chocolatey for as much as I possibly can, because you can update everything in one fell swoop.
After that, editing things from PowerShell is a snap. Like my PowerShell profile:
notepad++ $PROFILE
Hope this helps someone, or several someones!
In my case, I wanted to start Notepad++ with a file as an argument, and open as admin. I wanted to open one of the PowerShell profiles. I had to use the following command variation:
start-process -Verb runas -filepath "C:\Program Files (x86)\Notepad++\notepad++.exe" "`"$($PROFILE.AllUsersAllHosts)`""
All the other variations didn't work, I think due to a space in the path of the file to be opened. So, you must escape the " as:
"He said `"This is fun.`""