# Running scripts in any computers

**URL:** <https://forums.powershell.org/t/running-scripts-in-any-computers/1023>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/running-scripts-in-any-computers/1023 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/running-scripts-in-any-computers/1023/1 "2011-12-31T18:30:00Z")

</div>

by vandreytrindade at 2012-11-26 06:11:57

> Hi,  
>   
> I want to create a script that runs no matter where they are in the computer.  
>   
> Example:  
>   
> **Computer 1**  
>   
> User have: C:\Scripts\Working\PSmain\_script.ps1  
> Inside that script, there’s a command to run another script in the same directory ".\PSdir\_script.ps1".  
>   
> After testing the script, the user sends those two scripts files to another user by e-mail.  
>   
> **Computer 2**  
>   
> The other user recieves the mail then save the scripts to C:\Temp\ directory.  
> PS: This user has a PowerShell profile with the command "Set-Location C:&quot; in it.  
>   
> So… when he tries to run the script it gives an error telling that the ".\PSdir\_script.ps1" wasn’t found, because there isn’t any file in the C:\ called "PSdir\_script.ps1".  
>   
>   
> I’ve tried to use the "Get-Location", but it always gave me the "C:&quot; path, no matter the source of the script.  
> Any 1?  
>   
> I’m trying to use this solution to get my script working:  
> [viewtopic.php?f=9&t=794](https://powershell.org/discuss/viewtopic.php?f=9&t=794)

by powerschill at 2012-11-26 06:17:45

> You can use the following code to retrieve the location of the script that is being executed:  
>   
> `$ScriptDirectory = Split-Path $MyInvocation.MyCommand.Definition -Parent | Split-Path -parent`

by vandreytrindade at 2012-11-26 06:27:36

> Hi, thanks for the quick response, but I get this error:  
>   
> "PS C:\Active Directory\> $ScriptDirectory = Split-Path $MyInvocation.MyCommand.Definition -Parent | Split-Path -parent  
> Split-Path : NÃ£o Ã© possÃ­vel associar o argumento ao parÃ¢metro ‘Path’ porque ele Ã© uma cadeia de caracteres vazia.  
> No linha:1 caractere:76  
> + $ScriptDirectory = Split-Path $MyInvocation.MyCommand.Definition -Parent | Split …  
> +  
> ~~~~~  
> + CategoryInfo : InvalidData: (:PSObject) [Split-Path], Parameter BindingValidationException  
> + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.SplitPathCommand  
>   
> PS C:\Active Directory\>"  
>   
> **Error in english: Split-Path : Is not possible to associate the argument to the parameter ‘Path’ because it is a null chain… or something like this =p**

by powerschill at 2012-11-26 06:30:38

> You have to put that code in the script that calls the other script. It won’t work from the command prompt.

by vandreytrindade at 2012-11-26 06:43:44

> I’ve created a script with the line you gave to me:  
>   
> **Import-Module ActiveDirectory  
> $ScriptDirectory = Split-Path $MyInvocation.MyCommand.Definition -Parent | Split-Path -parent  
> $ScriptDirectory  
> pause**  
>   
> Opened Windows Explorer, went to the "Active Directory" directory and clicked twice in the test.ps1 file.  
> The result of the $ScriptDirectory variable is the "C:&quot; and not the "C:\Active Directory" folder.

by powerschill at 2012-11-26 06:51:43

> [quote]Opened Windows Explorer, went to the "Active Directory" directory and **clicked twice in the test.ps1 file**.[/quote]  
>   
> You aren’t able to double-click a PowerShell script to execute it. Did you make any changes to your system to make this possible?

by vandreytrindade at 2012-11-26 07:31:07

> How I’m not enabled to do that?  
> I always use my scripts in that way…  
>   
> I only choose to open in PowerShell all the ps1 files…

by vandreytrindade at 2012-11-26 07:35:57

> It worked!  
> What happened is that the command you gave me removed the directory that I wanted to be used: "C:\Active Directory"  
>   
> "$ScriptDirectory = Split-Path $MyInvocation.MyCommand.Definition -Parent | **Split-Path -parent**"  
> So I removed that part and tested like that:  
>   
> **$ScriptDirectory = Split-Path $MyInvocation.MyCommand.Definition -Parent  
> $actualdir = $ScriptDirectory.Remove(0,2)  
> cd $actualdir  
> dir**  
>   
> Thank you so much 😃

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:02pm UTC](https://forums.powershell.org/t/running-scripts-in-any-computers/1023/2 "2024-05-16T21:02:50Z")

</div>


