How to run a Powershell script from the command line and pass a directory as a parameter

PowershellPowershell 2.0

Powershell Problem Overview


PowerShell -Command .\Foo.ps1
  • Foo.ps1:

     Function Foo($directory)
     {
         echo $directory
     }
    
     if ($args.Length -eq 0)
     {
         echo "Usage: Foo <directory>"
     }
     else
     {
         Foo($args[0])
     }
    

Despite Foo.ps1 being in the directory from where I am calling Powershell, this results in: none The term '.\Foo.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

  • EDIT: Wasn't working because PowerShell was changing directory due to profile.ps1 containing cd C:\


I then tried to call it specifying the full path to the script file, but no matter what I try, I can't get it to work. I believe I have to quote the path because it contains whitespaces, as does the file name I need to pass in an argument to the script.

  • Best guess so far:

     PowerShell -Command "'C:\Dummy Directory 1\Foo.ps1' 'C:\Dummy Directory 2\File.txt'"
    

Outputs error: none Unexpected token 'C:\Dummy Directory 2\File.txt' in expression or statement. At line:1 char:136.

Powershell Solutions


Solution 1 - Powershell

try this:

powershell "C:\Dummy Directory 1\Foo.ps1 'C:\Dummy Directory 2\File.txt'"

Solution 2 - Powershell

you are calling a script file not a command so you have to use -file eg :

powershell -executionPolicy bypass -noexit -file "c:\temp\test.ps1" "c:\test with space"

for PS V2

powershell.exe -noexit &'c:\my scripts\test.ps1'

(check bottom of this technet page http://technet.microsoft.com/en-us/library/ee176949.aspx )

Solution 3 - Powershell

Using the flag -Command you can execute your entire powershell line as if it was a command in the PowerShell prompt:

powershell -Command "& '<PATH_TO_PS1_FILE>' '<ARG_1>' '<ARG_2>' ... '<ARG_N>'"

This solved my issue with running PowerShell commands in Visual Studio Post-Build and Pre-Build events.

Solution 4 - Powershell

Add the param declation at the top of ps1 file

test.ps1

param(
  # Our preferred encoding
  [parameter(Mandatory=$false)]
  [ValidateSet("UTF8","Unicode","UTF7","ASCII","UTF32","BigEndianUnicode")]
  [string]$Encoding = "UTF8"
)

write ("Encoding : {0}" -f $Encoding)

result

C:\temp> .\test.ps1 -Encoding ASCII
Encoding : ASCII

Solution 5 - Powershell

Change your code to the following :

Function Foo($directory)
    {
        echo $directory
    }
    
    if ($args.Length -eq 0)
    {
        echo "Usage: Foo <directory>"
    }
    else
    {
        Foo([string[]]$args)
    }

And then invoke it as:

>powershell -ExecutionPolicy RemoteSigned -File "c:\foo.ps1" "c:\Documents and Settings" "c:\test"

Solution 6 - Powershell

you have type and hit enter :

PowerShell -Command

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPolyfunView Question on Stackoverflow
Solution 1 - PowershellCB.View Answer on Stackoverflow
Solution 2 - PowershellLoïc MICHELView Answer on Stackoverflow
Solution 3 - PowershellTom 'Blue' PiddockView Answer on Stackoverflow
Solution 4 - PowershellHyundong HwangView Answer on Stackoverflow
Solution 5 - PowershellPrasoon DwivediView Answer on Stackoverflow
Solution 6 - PowershellSachin MishraView Answer on Stackoverflow