how to set location path for the script

Welcome Forums General PowerShell Q&A how to set location path for the script

This topic contains 6 replies, has 2 voices, and was last updated by

 
Participant
2 years, 10 months ago.

  • Author
    Posts
  • #34526

    Participant
    Points: 0
    Rank: Member

    Hi,

    I have a script in which i have to set the location path for the software from where it will executed. Currently what i am doing is something like this –

    $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
    Set-Location C:\Users\ankitpar\Downloads\nuget.exe Install WindowsAzure.storage -Version 1.7.0.0

    I don't want to give the whole hardcore path. Just want to define in powershell language so kindly suggest. Any Insights would be appreciated.

    Thanks
    Ankit

  • #34531

    Participant
    Points: 0
    Rank: Member

    Hi Ankit,

    You could use $PSScriptroot if your using version 3 or above.

    Here is a snippet from About_Automatic_Variables help topic:

    $PSScriptRoot
    Contains the directory from which a script is being run.

    In Windows PowerShell 2.0, this variable is valid only in script modules
    (.psm1). Beginning in Windows PowerShell 3.0, it is valid in all scripts.

    so if you run the script from root of C (Which you normally don't, just an example) you could use it like this:

    $PSScriptroot\Users\ankitpar\Downloads\nuget.exe

    Does that help ?

  • #34532

    Participant
    Points: 0
    Rank: Member

    Thanks for the reply.. I have done what you have suggest like given below –

    $PSScriptroot\Users\ankitpar\Downloads\nuget.exe Install WindowsAzure.storage -Version 1.7.0.0

    but getting error below so could you suggest me about this?

    At line:1 char:14
    + $PSScriptroot\Users\ankitpar\Downloads\nuget.exe Install 'WindowsAzure.storage – ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Unexpected token '\Users\ankitpar\Downloads\nuget.exe' in expression or statement.
    At line:1 char:14
    + $PSScriptroot\Users\ankitpar\Downloads\nuget.exe Install 'WindowsAzure.storage – ...

    Thanks

  • #34534

    Participant
    Points: 0
    Rank: Member

    If you run it as a one-liner it won't work. Try wrapping it in a function a little function:

    Function set-path {
    
    #set the location path for the software install
    $PSScriptroot\Users\ankitpar\Downloads\nuget.exe
    
    }
    
    

    This is the basic of the basic as i don't have time to make it all singing all dancing. But should run to show you how it works.

  • #34535

    Participant
    Points: 0
    Rank: Member

    I have executed like below –

    Function set-path {
    $PSScriptroot\Users\ankitpar\Downloads\nuget.exe Install WindowsAzure.storage -Version 1.7.0.0
    }

    Still giving error –

    At line:3 char:14
    + $PSScriptroot\Users\ankitpar\Downloads\nuget.exe Install WindowsAzure.storage -V ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Unexpected token '\Users\ankitpar\Downloads\nuget.exe' in expression or statement.
    At line:3 char:14
    + $PSScriptroot\Users\ankitpar\Downloads\nuget.exe Install WindowsAzure.storage -V

    Thanks
    Ankit

  • #34539

    Participant
    Points: 0
    Rank: Member

    This will set the folder :

    Function Set-Folder {
    
    #Set folder 
    $Directory = resolve-path $PSScriptRoot\temp 
    
    #Demonstrate result
    $Directory
    
    }#end of function
    
    

    Not sure what you are doing with the nuget.exe. Is "Install WindowsAzure.storage -Version 1.7.0.0" part of the syntax to use with nuget.exe ?

  • #34563

    Participant
    Points: 0
    Rank: Member

    hey,

    thanks and sorry.. i am newbie in powershell so not much knowledge .. actually here i am trying to install package of azure though nuget.exe. nuget is the package for the microsoft development platform and as azure is microsoft product so i am trying to install azure through that.

    Thanks & Regards
    Ankit

The topic ‘how to set location path for the script’ is closed to new replies.