Powershell
Tips and Tricks
Updated: 02 October 2023
Modify Prompt
Based on this
Open Powershell and type
If the file does not exist you will be asked to create it, then paste the following
You can replace the $p >
part with anything you’d like to be displayed in your shell
Alternatively, if you would like to see the full path, and the command on the next line you can use
Adding Aliases
Based on this
We can add persistent aliases in our profile by defining functions for them, we can open our Powershell Profile
Then we can define a function to navigate to our Repo directory by adding the following to the profile
View File Tree
You can view the folder and file tree in powershell using the tree
command.
To view the Folders only in the current directory:
Which will output something like:
Or from another directory you can use:
To refer to the current directory you can use
tree
without any directory, ortree .
to refer to the current directory. Both ways are almost identical in terms of the output. Note that including the directory in the command results in the full directory path being written in the command’s output
And to view the tree with files included, you can use:
Which will output something like:
And for another directory:
Symlinks
Support for
mklink
only exists on newer versions of Windows, otherwise you may need to useNew-Item
Creating symlinks can be done with the mklink
command which is an alias for the New-Item -ItemType SymbolicLink
command
Using the base New-Item
version, a symlink can be created with the below command where data.txt
is the linked file we want to create and `./main/data.txt/ is the path to the existing file:
Which creates a file called data.txt
which is linked to ./main/data.txt
The mklink
version of this is:
For more info see [this page](https://superuser.com/questions/1307360/how-do-you-create-a-new-symlink-in-windows-10-using-powershell-not-mklink-exe_
Git Status for Sub-Directories
Check which folders have modified files (only goes one level deep)
Zip and Unzip a File
Copy and Paste Files
Copy the current directory with:
Copy a single file with:
Paste Item with:
Send an Email
From this StackOverflow Answer
Troubleshooting Path Commands
If a command is not running the application/version of the application you’d expect you can try the following steps:
- Refresh your path and try the command again, it may just not be up-to-date. You can use the following function to do that:
- If you command is just generally being weird in some way it may be because it is defined/part of another path than the one you expect
To see where a command is being run from, you can try the following:
e.g. for node:
e.g my
node
command was being problematic because it was running the one in my Anaconda path entry (because apparently Anaconda comes with Node)
- In order to ensure that the correct application/command takes precedence move it higher up your path
Create Drive Aliases
On Windows you can alias specific folders as virtual drives (for easy reference or to get around file length restrictions)
To view your aliases/drives you can run:
To create a new drive you can use:
Where X
is the drive you would like to map to. Be sure not to have the trailing \
in your folder name
The path to the folder can also be relative:
.\my\rel\path
To remove a drive run the following command:
What Process is Using a Port
To view what process is using a specific port you can run the following command:
You can also use the following command:
Killing Process by ID
If you have the PID, for example using one of the above commands, you can kill us using taskkill
, for example for killing PID 1234
Zipping Files
To zip all the files in a directory you can use the following:
To zip a directory, including the root directory in the Zip use the following:
Search for Devices on Network
To view the IP’s of the different devices that are currently connected to your network you can use the following command:
Connect to RDP
To use an RDP file you can make use of the mstsc
command with a path to the RDP file you want to connec with:
Which will then open the RDP file
Switch to Home Directory
You can switch to your home directory on Powershell with cd ~
, the ~
directory represents the user’s home, same as in other OS’s and shells
My Current $PROFILE
Yout Powershell is a script that runs whenever you open a new powershell instance, the functions available in it become part of your session so you can just call them from the terminal. The path to this file is stored in every Powershell instance as the $PROFILE
variable
To open your $PROFILE
in notepad you can run:
My current profile which has some common commands is here: