Why is the PowerShell prompt different when opened from a directory? - windows

I have the habit of using the caps+right click option "Open PowerShell prompt here", and I noticed it doesn't have the same prompt as when I open it from the start menu:
The font is way bigger when opened from a directory
The PATH used seems to be different (which is the main problem here)
For the second point, I have an foo.exe with its directory in the PATH (both System and my current user).
If I open PowerShell from the start menu, type "foo.exe", it executes.
If I use "Open PowerShell here" and type "foo.exe", I get a CommandNotFoundException.
What I would like is both to understand why there is a difference, and how to put my directory somewhere where I can "foo.exe" in any context.
PS: I'm using Windows 10 Creators Update

Related

Change location to shortcut location

I am launching a script with a powershell shortcut (C:\Windows....\powershell.exe -file 'D:\powershell\script.ps1').
Is there a way to make script change its current location to location of said shortcut?
Example: Script itself is in D:\powershell\ and the shortcut is in C:\Work\Project1. I need the script to cd to "C:\Work\Project1\".
Thanks
You can make a shortcut that starts in whatever directory it's located in. All you have to do is modify the "Starts In" property of the shortcut, and blank it out. That's right. Set it to nothing.
Then, when you want to invoke the shortcut, navigate to the folder (directory) where it's located before invoking it. I use this technique for a shortcut that launches powershell but doesn't launch a script. I haven't tested it with a shortcut that launches a script.
Walter Mitty's helpful answer provides an effective solution, provided that the shortcut file is opened from either the Desktop or File Explorer.
This answer provides background information.
You must configure the desired working directory as part of the shortcut file, because the script you invoke knows nothing about the shortcut that invoked it.
Therefore, to configure a specific working directory (e.g., C:\Work\Project1), specify it in the shortcut file's Properties dialog in the Start in: field.
In case you want to update a shortcut file's (*.lnk) working directory programmatically, use the technique from this answer with the .WorkingDirectory property.
Note: In both cases, only an absolute path can be configured as the working directory: File Explorer only allows you to enter an absolute path anyway, and while the programmatic method allows you to assign a relative path, it is instantly resolved to an absolute one, relative to the shortcut's location.
To make the shortcut's own directory the working directory, you can blank out the Start in: field / .WorkingDirectory property, but note the limitations:
Only works when such a shortcut is either opened from either the Desktop or from File Explorer.
By contrast, opening it from the taskbar or (pre-Windows 10 only) the Start Menu, the working directory is $env:windir\System32 (typically, C:\Windows\System32).
If the shortcut targets an application (rather than a document), as in this case, that application invoked by the shortcut - including cmd.exe and powershell.exe - starts in the configured / implied working directory.
Caveat re cmd.exe: If the working directory is specified as as a UNC path, cmd.exe won't be able to change to that directory; as a workaround, use a path with a mapped drive instead (but, obviously, that drive must be mapped at the time the shortcut is opened).
You can use the TargetPath property of the shortcut (maybe do a get-childitem $psscriptroot where name is like scriptname and extension is like .lnk) with the set-location cmdlet
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-location?view=powershell-6

How can I open command prompt in the current directory as a shortcut

I'm just wondering how I can make a shortcut that will open command prompt in the folder it is currently in.
Why I need this.
I save my c program file in the directory Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\
So I would have to type cd Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\ every time I want to open the command prompt in that directory.
Is there a shortcut I can make where I can move that shortcut around anywhere (don't need to change the shortcut) and it will open command prompt in the directory it is current in, so I don't need to keep typing cd .....
you can go to the directory and just type start . in the same directory
or you can make a batch file like test.bat and add below line in the same:
cd Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\
Save it anywhere and whenever you will run that batchfile, it will automatically move PWD to the required directory
Right-click in the open space of your desktop and click New > Shortcut
For the location, type or copy and paste one of the following lines.
%windir%\system32\cmd.exe /k cd Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\
As commented by aschipfl, you can already do it, (may be OS dependent)!
In Windows Explorer, or I suppose the desktop, just hold down the shift key, right click in a blank area of it and choose Open command window here, (or whatever your locale states).
I think in Windows 10 at least, the option is even available in the Explorer toolbar menu too.

Executing a command-line .exe file

I have a .exe file converted from a .jar.
It is a command based application, so I have to start it with a batch script. Here is the batch script:
#echo off
cd C:\desktop\plant-text-adventure-win
start planttextadventure
pause
When I double click on the batch script, this happens: Windows could not find 'planttextadventure'. Please confirm if you have input the correct name and retry.
I don't know what is happening, I have no idea about cmd as I use Mac, but I can confirm I have an executable called planttextadventure.exe in a folder called plant-text-adventure-win.
You should test your batch file by executing it within a shell.
Simply enter within the start menu the command cmd to open up a shell. Within this black box you could now simply enter the commands from your batch script and lookout for some error message.
If you look at your script I would guess that the cd command (to change the current directory) is not correct. Maybe you should replace it with
cd %USERPROFILE%\Desktop\plant-text-adventure-win
because the desktop folder is on a default installation not directly under the root drive but within the user profile available.
Another solution to get this thing to work, is by opening the windows explorer, going to the .exe file you wish to execute and drag & drop the .exe file with a right mouse click onto the desktop.
Then a context menu appears and you select the option Create shortcut here.

How do you run programs in Windows terminal?

What is the windows equivalent to "./filename"
So for example I would usually compile by doing something like:
gcc -c homework1.c
gcc -o homework1 homework1.o
This would give me the executable names homework1
And for me to run the program, I would type: ( ./homework1 ) <-- ignore the parenthesis.
Usually I was write all my code in my schools Unix Shell thingy and I also compile it and run it there, but recently I think I took up all the disc space (because it says "disc quota exceeded").
Run cmd.exe
Go to where the program is example : cd C:\foder1\
Then type the program name with extension, for example : test1.exe or "test1.exe"
In windows (as in Linux) you can either run a program though a GUI interface or from a shell environment.
The GUI option is a program called Explorer, you navigate through the file system and double click executable files to run then. Executable typically have the extension '.exe' or '.bat', but there are others.
The shell environment in windows is called the 'command prompt', you can run it by going to the start menu and selecting 'run' or simply press the windows key and 'r' simultaneously. A box will popup, type 'cmd' (without the quotes) and hit enter - the command prompt should open. From there you can navigate the file system using commands like 'cd'. To run your executable type the name of the file (it should work with or without the '.exe').
A nice shortcut to open the command prompt already at a particular path, is to browse to the folder in Explorer, hold shift and then right-click the folder - the resulting context menu that pops up should have an option like 'open in command prompt'.

File Access takes me to command prompt rather than Explorer

Whenever I go to my command prompt and type a "c:\temp" or any other file/directory, it takes me to the command prompt rather than the file explorer. Can somebody please tell me how I can fix this so that all file/directory access from cmd takes me to the file explorer.
I have a Win2k3 64bit machine. Has this got anything to do with the 32bit/64bit explorer ?
To run Windows Explorer from command prompt you must type name of the executable:
explorer.exe
or just
explorer
To open a certain folder, pass the path as an arguments. Details here:
Command-Line Switches for Windows Explorer (MSDN KB)
See examples.
To open Explorer at the directory where your cmd is operating you can use
start .
and at a parent directory you can use
start ..

Resources