Windows Powershell: Shortcut for change directory - shell

I just started using Windows Powershell and one major problem I've run into is when I start it, it starts me off in this directory:
C:\Users\Username
However, the directory I usually need to navigate to is in something like:
C:\Users\Username\Dropbox\Websites\2014\Projects\ProjectName
And sometimes it goes much deeper. So you can see how it is a little annoying to navigate each time I start the shell to this directory using ten separate cd commands. I was wondering if there was a way to set up a shortcut or alias for something like:
cd C:\Users\Username\Dropbox\Websites\2014\Projects\ProjectName
Or possibly I could set some sort of shortcut to the directory only so I could do something like:
cd shortcut
And it would cd to the proper directory. Does anyone have any experience with something like this? Is this a dumb thing to do? I'm very new to using any command line so I'm just trying to get used to navigating around files and folders more easily.

There is also another way to do this:
function PP { cd C:\Users\Username\Dropbox\Websites\2014\Projects\ProjectName }
Set-Alias shortcut `PP`

new-psdrive is your friend :
New-PSDrive -Name docs -PSProvider FileSystem -Root "C:\Users\username\Documents"
cd docs:

Run this in powershell:
start notepad $profile
That will open up your profile in notepad (notepad will prompt you to create it if it doesn't exist).
Any code you write in this .ps1 file will be executed when powershell starts.
You can also set a system environment variable, like if you were to set MYPSPATH equal to C:\Users\Username\Dropbox\Websites\2014\Projects then you could do this:
cd $env:MYPSPATH
That could be done either manually each time, or automatically within your $profile.
Also it's unclear from your question, but it sounds like you're doing a cd for every path component.
There's no need to do that. This command that you wished for:
cd C:\Users\Username\Dropbox\Websites\2014\Projects\ProjectName
will work as is. If I misunderstood this point I apologize.
Something that also might be useful to you is pushd which is an alias of Push-Location. This lets you change to a new directory and easily revert back where you started with popd or Pop-Location.
PS C:\users\Xenostar> Push-Location .\Dropox\Websites\2014\Projects
PS C:\users\Xenostar\Dropbox\Websites\2014\Projects> Pop-Location
PS C:\users\Xenostar>
And you can push multiple levels deep, and keep popping back to the previous ones.

I believe this is the documentation you are looking for to change the default location where powershell starts up: The Windows PowerShell Profile

Although it doesn't directly answer the question, I found using this utility makes navigating around in powershell far easier: https://github.com/rupa/z
Install using Install-Module z -AllowClobber
Then navigate with z whatever where it will match "whatever" against your most frequently visited folders.

If the directory you want to go is named X; then write cd X:\

Related

How to call PowerShell script from WSL?

In my Windows directory
C:\Users\jholmes\pichak\analytics
I have run1.ps1 code.
I run wsl.exe, now my pwd is
/mnt/c/WINDOWS/system32
How to point to the first path and execute the script?
In WSL2, using the -File option worked for me:
powershell.exe -File path/to/script.ps1
When running wsl (or wsl.exe) from PowerShell, it should start in the same directory that you were in under PowerShell, just with the Linux version of it. For instance, if you are in PowerShell in C:\Users\jholmes\pichak\analytics, and you run wsl, you should end up in /mnt/c/Users/jholmes/pichak/analytics.
If not, then you may have something in your startup file already like the other answer recommend. You should remove any cd commands from your .bashrc, .zshrc, .profile, .bash_profile, or any other startup file you may have edited.
The wsl command provides a few handy arguments for specifying the starting directory:
wsl ~ will start in your Linux user's home directory (e.g. /home/jholmes)
wsl --cd c:\ will start in /mnt/c (or whatever the equivalent Linux directory is to the Windows version passed in).
wsl --cd \\wsl$\<distroname>\etc will start in your /etc directory (or whatever WSL path you passed in. Note that you will need to specific your distribution name (obtained from wsl -l -v).
Let's say that you are in /home/jholmes, and you want to execute the PowerShell script C:\Users\jholmes\pichak\analytics\run1.ps1. You first need to translate the Windows path to the Linux version:
/mnt/c/Users/jholmes/pichak/analytics/run1.ps
You can then run it via:
powershell.exe /mnt/c/Users/jholmes/pichak/analytics/run1.ps
It's also possible to set up a PowerShell script with a "shebang" line to execute it directly. If you add the following as the first line of run1.ps1:
#!/usr/bin/env -S powershell.exe -ExecutionPolicy Bypass
Then set it to be executable via chmod +x /mnt/c/Users/jholmes/pichak/analytics/run1.ps, then it becomes a "command" of sorts (technically, an "executable script") that you can execute directly by just typing the fully qualified name of the script at the command line:
/mnt/c/Users/jholmes/pichak/analytics/run1.ps
or
cd /mnt/c/Users/jholmes/pichak/analytics
./run1.ps
There are two ways to go about this:
You can change your working directory to that of your shell script and execute it normally. To do so, follow these steps:
Mount the relevant drive cd /mnt/c/.
Change directories according to the path of the script.
This approach is more of a hack that I use for the sake of convenience. I have created a folder in my Windows storage wherein I store all Ubuntu WSL related files. Say, D:\Ubuntu. To avoid changing the working directory every time you open WSL, you can modify the shell profile file (bashrc, zshrc etc.) to load the relevant directory at the end.
i.e., Add cd /mnt/d/Ubuntu/ at the end of your ~/.zshrc file if you use zsh or the relevant profile file otherwise.
Here is the evidence, that with the WSL2, our software can make the Powershell script encrypted and protected:
See this picture:
And it can detect and kill the process that uses fanotify because we think it can be used by attackers to hide critical file change (it also can find and kill dtrace/strace/stap/bpftrace/debuggers and process that opens /dev/kmem, /dev/mem, /proc/kcore, and the protected process' /proc/pid/mem etc):
This picture shows the encrypted PowerShell script now can detect and kill possible attackers' processes
For me this answer was almost the good one but when running for instance:
powershell.exe -File /mnt/c/path/to/script/script.ps1
I got following error:
The argument '/mnt/c/path/to/script/script.ps1' to the -File parameter does not exist. Provide the path to an existing '.ps1' file as an argument to the -File parameter.
I then have to:
cd /mnt/c/path/to/script
powershell.exe -File script.ps1

how to make a Powershell script avaiable from anywhere

I have a Powershell script I would like to make "public", meaning I want to be able to execute the script from any folder as you can do from the command prompt.
Is this possible?
You can also add an alias in your local powershell profile
Alias Example in profile
Set-Alias hello C:\scriptlocation\script.ps1
Now anytime you type hello, the script.ps1 will run.
More info on the various profiles that the alias can be saved to.
https://devblogs.microsoft.com/scripting/understanding-the-six-powershell-profiles/
You can explore the use of your powershell profile to achieve this. See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles.
If the script is just a function or some variables, you can copy and paste the content into your profile.
Alternatively, if the script represents a standalone unit of code you want to keep separate, you could import it into your main profile as such:
get-content -path C:\blahblahblah\scriptName.ps1 -raw | invoke-expression
Finally, if you are writing an "advanced" powershell function, or are trying to do things officially, you could investigate the use of powershell modules as a way to export your function. See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules
#Lee_Dailey's answer of adding the script to your path is also viable. If you want to add a lot of scripts, one way to do that is to add a folder like C:/PowershellScripts/, to your path, save your scripts there, and then you'll be able to invoke your .PS1 file from anywhere.
Name the script something meaningful ie: awesome.bat Save it in a dir and add the dir to windows env. Command awesome will be globally available.

How to run VSCode from the command prompt

I for security reasons cannot run VSCode plainly. I have opened it in the past, but now due to specific reasons, I may only run VSCode from the command prompt. I've tried
start "path/to/file" code and start code "path/to/file"
but none work I'm on Microsoft Windows [Version 10.0.17134.407]
how may I run this by going to Windows+R then 'cmd' then start/ run?
Also it would be great if I could use this for a separate user.
I'm looking for something like:
Runas /user:user\admin /savecred "C:\Program Files (x86)\vs-code.exe"
The use of start is useless if VSCode is in the environment variables.
You can use code C:\Users\%username%\Desktop\File.c for exemple.If it doesn't work, I advise you to use a vbs script instead
You also don't need to run VSCode as an administrator unless you need to edit a file in a protected folder.
Maybe not the exact answer to the question, but...
To start Visual-Studio-Code from CMD into the current folder write:
code %cd%
The environment variable cd tell VS-Code to open it with the current folder
just open a cmd terminal and type code followed by
just open a cmd terminal and type code followed by return keyborad key.
Well shoot, as it turns out that after doing some experimentation I found out that there's a way. Do this:
Simply stick this:
runas /user:Techtiger255\admin /savecred "C:\Users\Admin\AppData\Local\Programs\Microsoft VS Code\Code.exe"
inside of a shortcut (.lnk file)
Open your command line of choice (Powershell or Cmd) and enter the exact file path of your shortcut ex:
"C:\Users\Standard\Desktop/VSCODE.lnk" and hit go, stupidly simple really, just had to find the code.exe file path.

cmd filename as command in cmd.exe

I am probably missing some lingo to find the answer to a question which I believe has probably been posted already, hence my apologies beforehand.
I wrote a cmd script (myCmdFileName.cmd) that changes some IP configurations using netsh. To run it from cmd, I have to be in the same folder of the script and write in the console myCmdFileName.cmd.
As you know, netsh is a commandline application itself, as I would use it like:
netsh [-flags] [-configs]
What I would like to do is to have my cmd file be called just like as if it was an application like netsh, not worrying about whether I am in the file folder, so like this:
myCmdFileName [-flags] [-arguments]
How would I go about doing that in Windows cmd?
Thanks a lot in advance!
In order to run myCmdFileName from anywhere, either it's parent folder needs to be added to %PATH%, or you can move it / create a symbolic link to it in a folder that already is in %PATH%.

How to launch babun in a specified directory via the command line

now I'm currently using Visual Studio Code and I wanted to make the terminal use babun instead of the default cmd.exe
In fact, I have already managed to do that in theory - the problem is that, unlike with cmd.exe, the directory that I'm in upon starting the terminal is no longer the currently open project, it's just simply /home/myusername (i.e. a path in babun's directory tree).
This kinda sucks, since I don't really want to navigate to my directory every time.
Next, I also found out how to tell babun (in my case, zsh.exe) to use a default directory such as C:/ by adding cd /cygdrive/c to the end of .zshrc - Unfortunately, that's not what I want either, since I don't want babun to always use the same directory.
Now I figured that, seeing as this would be the most comfortable option with VSC*, there might be some console argument that tells zsh.exe to use a specific directory. Sadly, I couldn't find anything, hence this question.
Weirdly enough
Anyone know how to help me out? Appreciate the help :)
*VSC allows you to specify the path to your shell, as well as an array of arguments that will be passed.
You could place your directory into a cmd file and run it as a shell:
d:
#rem note that's important to change the drive 'permanently'
cd d:\home
zsh.exe
And then specify this cmd file as shell to invoke.
Open the desired directory in a file explorer, right click, then select Open Babun here.
Babun currently does not natively support a command line parameter specifying the directory to launch in.
However, there is a pull request in the Babun GitHub which solves the issue. Unfortunately, it doesn't seem to be likely to be accepted anytime soon, though.
To gain access to the feature manually, check out the pull request.

Resources