how to make a Powershell script avaiable from anywhere - windows

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.

Related

Is there a Windows 10 PowerShell equivalent to .bashrc for Linux Bash?

I want to run some commands by default every time I open up power shell. Bash has a .bashrc file that it runs as source when it starts up. Is there a way to do this with powershell?
As pointed out by Dai in the comment section, Powershell Profiles sound like the closest equivalent to the .bashrc file.
A PowerShell profile is a script that runs when PowerShell starts. You can use the profile as a logon script to customize the environment. You can add commands, aliases, functions, variables, snap-ins, modules, and PowerShell drives. You can also add other session-specific elements to your profile so they are available in every session without having to import or re-create them.

How to set ps as shortcut to open powershell

The question says it all. I want to learn and use powershell as my go to terminal in windows and I want powershell to open if I type Win+R followed by ps. Much like how cmd is used to open command prompt.
You can create a symbolic link (similar to a shortcut) to Powershell with any name you want.
This will create one called ps.exe in the powershell folder, this folder is already listed in PATH so will enable you to run ps from the RUN box like you want.
mklink %SystemRoot%\system32\WindowsPowerShell\v1.0\ps.exe %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Be sure to run the command in an elevated command prompt.
The "default" command for opening PowerShell is powershell. If you want to make it ps, your best bet is to create a batch file named ps.bat containing the single line
#powershell %*
and place it in one of the directories named in your system PATH variable.

Powershell issue when executing .ps1 script

So, warning, this is probably a really newbie questions, so apologies in advance.
I'm starting to learn Powershell and one of the first things I want to do, is just make a directory & copy a file to it.
Now, if I use the following commands in a CMD window, they work perfectly.
mkdir %HOMEPATH%\test
cp test.txt %HOMEPATH%\test
However, when I put them into a .ps1 file and execute it, I get an error saying the directory could not be found etc (see below)
Copy-Item : Could not find a part of the path 'C:\Chef\windowsdevbox-master\%HOMEPATH%\.berkshelf'
Now, I got told that this is because I need to put CMD before each command. I ran this, with the CMD in front of each one and the error disappeared and instead I was presented with the "Home text" for CMD and the script finished.
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
However, the folder was not created and the file was not copied over.
I just wondered what I need to do to get this to work.
In PowerShell mkdir is a built in function designed to emulate the same functionality, but using the built in cmdlet New-Item to do the underlying work.
cp is a straight up alias to PowerShell's Copy-Item cmdlet.
You don't need to precede these with cmd to make them work.
PowerShell does not accept the %VAR% syntax for environment variables though. Instead you would use the special $env variable, followed by a colon : followed by the variable name: $env:HOME.
mkdir $env:HOMEPATH\test
cp test.txt $env:HOMEPATH\test
%HOMEPATH% is not a PowerShell variable.
Environment variables are stored in the $env variable scope. You can access it with $env:homepath.
Here, I would use:
mkdir "${env:homepath}\test";
cp test.txt "${env:homepath}\test";
I might be inclined to use mkdir "${env:homedrive}${env:homepath}\test";, but I don't really know what you're trying to accomplish.
The curly braces here tell PowerShell that the entire contents are the variable name. The colon tends to confuse it, IMX, especially when you embed variables in strings.
Environment variables are special in PowerShell. They have their own provider. You can list them with Get-ChildItem env:, and manipulate them at the env: PSDrive.
Additional Note: Certain configurations might have unpredictable results because the strings might have too many backslashes or have backslashes in the wrong place. In this case, you might need to use Join-Path to combine the paths correctly.
Say:
$env:homedrive is 'U:'
$env:homepath is '\'
And the subfolder is '\test'
Then "${env:homepath}\test' is \\test, which looks like a UNC path. Instead you can use Join-Path ${env:homepath} '\test', which will then correctly create '\test'.
If you want to join three things, it's a bit complex:
Join-Path ${env:homedrive} (Join-Path ${env:homepath} '\test')
But that correctly creates 'U:\test' instead of 'U:\\test'.
You can also use the -Resolve option to convert a relative path to the absolute one.

Windows Powershell: Shortcut for change directory

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:\

How do I create a Powershell alias (or function, or cmdlet) that runs in the same process as my shell?

In Unix, I create a lot of aliases/functions. Here is my workflow:
* I have a file named 'aliases.txt' in my $HOME directory
* I have a quick command to edit that file
* I have an alias named 'sa' that sources that alias file. I.e.
alias sa='. $HOME/aliases.txt
So I can quickly and easily create, modify, and use new aliases.
I'm trying to re-create the same work flow in Powershell and it doesn't work. I've got the aliases.ps1 file in my $HOME directory. I tried creating an 'sa' alias, but of course a Powershell alias can't contain an argument. So I've tried
function sa {. $HOME\aliases.ps1 }
I can run it but changes in my aliases.pl1 file don't get reflected in my shell session. I'm assuming it's because Windows runs the 'sa' function in a new process, while Unix runs functions and aliases in the same process. How can I get my 'sa' back?
(BTW, "Type '. $HOME\aliases.ps1' at the command line each time" is not the answer I'm looking for.
Thanks in advance.
Would
. sa
be terse enough? You just need to dot-source the function call into the current scope. (Note there needs to be a space between the dot and the function name).
What are you trying to do? Some parts of your question doesn't make sense.
Ok, I understand better now.
Import-Alias is exactly for this purpose! Edit the file and do Import-Alias file and you get the new aliases that you have added.
Your flow:
Use the Set-Alias command to setup an alias and add that to your profile or just add that function to profile.
notepad $profile
(add the next in notepad)
function sa {. HOME\aliases.ps1 }
Now sa, should be available and the aliases.ps1 can be setup to use Set-Alias and add the aliases.
You are trying to map what you do in unix EXACTLY to Powershell. You should try and use what powershell has to offer:
Powershell flow:
notepad $profile
(in notepad)
Set-Alias <alias> <command>
Just add Set-Alias command to your profile.
Or use Export-Alias to export into a file and add Import-Alias to import from the file in your profile.
All these are excellently explained here: http://technet.microsoft.com/en-us/library/ee692685.aspx

Resources