Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

run powershell script from anywhere

I am currently working on a powershell script. The objective of this script is to import data from a .csv file from which new users are created if that username does not already exist in the Active Directory.

My question is how can I make this script run from any location so all I have to do is type the name of the script and it will run. I have been able to do this in BASH but can't figure out how to do this in power shell. So far google has been little help.

If it makes any difference i'm using Windows Server 2008 R2

like image 608
batsta13 Avatar asked Dec 16 '22 12:12

batsta13


2 Answers

The basic idea is to create Powershell Function which will do the work (or will call other script placed in other location) and put this method to Profile.ps1 script (the script which is loaded everytime you start powershell) - Look at Windows PowerShell Profiles for further details.

like image 152
Tomas Panik Avatar answered Dec 20 '22 05:12

Tomas Panik


The link above for Powershell Function from Tomas Panik is not there anymore so I want to add to the answer here.

Short version:

  1. You can create your function by using Powershell Function. However, this will only last for that session only.
  2. In order for you to use your function regularly, you need to generate/add your function to your own PowerShell profile. Quick tutorials are here and here. Tomas Panik's link to Windows PowerShell Profiles also has very good info.

Update: thanks Hussein Al-Mosawi for reporting the old broken link!

like image 25
KV88 Avatar answered Dec 20 '22 05:12

KV88