Running a Powershell command with a variable -Path parameter

powershell use variable as parameter
powershell pass variable to command
run powershell script with parameters from command line
powershell get-command parameters
powershell command line arguments
powershell pass multiple parameters to script
powershell pass variable to invoke-command scriptblock
powershell run remote script with parameters

Can someone please explain the following behaviour to me?

PS C:\Users\Kenny> $filePath = "C:\\Complicated.File.Path.That.Has.Special-Chars`[but-no.spaces`]and.definitely.exists\"
PS C:\Users\Kenny> cd $filePath
cd : Cannot find path 'C:\\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\' because it does not exist.
At line:1 char:1
+ cd $filePath
+ ~~~~~~~~~~~~~~~~                                 
    + CategoryInfo          : ObjectNotFound: (C:\\Complicated...initely.exists\:String) [Set-Location], ItemNotFoundE
   xception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

PS C:\Users\Kenny> cd 'C:\\Complicated.File.Path.That.Has.Special-Chars`[but-no.spaces`]and.definitely.exists\'
PS C:\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists> cd c:
PS C:\Users\Kenny> Write-Host $filePath
C:\\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\
PS C:\Users\Kenny> cd "$filePath"
cd : Cannot find path 'C:\\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\' because it does not exist.
At line:1 char:1
+ cd "$filePath"
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\\Complicated...initely.exists\:String) [Set-Location], ItemNotFoundE
   xception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

PS C:\Users\Kenny> cd ${filePath}
cd : Cannot find path 'C:\\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\' because it does not exist.
At line:1 char:1
+ cd ${originalPath}
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\\Complicated...initely.exists\:String) [Set-Location], ItemNotFoundE
   xception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

PS C:\Users\Kenny> cd ${$filePath}
cd : Cannot process argument because the value of argument "path" is null. Change the value of argument "path" to a
non-null value.
At line:1 char:1
+ cd ${$filePath}
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Location], PSArgumentNullException
    + FullyQualifiedErrorId : ArgumentNull,Microsoft.PowerShell.Commands.SetLocationCommand

PS C:\Users\Kenny> cd $($filePath)
cd : Cannot find path 'C:\\Complicated.File.Path.That.Has.Special-Chars[but-no.spaces]and.definitely.exists\' because it does not exist.
At line:1 char:1
+ cd $($filePath)
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\\Complicated...initely.exists\:String) [Set-Location], ItemNotFoundE
   xception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

If I had a penny for every minute I've wasted on PowerShell's insistance that backticks, quotes and other meta-characters are just right, I would be the richest man on Earth! Somebody please save me from this madness!

And BTW, what I'm actually trying to do is recurse through a bunch of folders and delete everything that isn't a video file, like so...

Remove-Item -Path $filePath -Recurse -Exclude '*.avi','*.mkv','*.mp4'

...which doesn't work for (presumably) the same reason: not being able to pass a variable to the -Path parameter. And if someone is feeling really generous, they could also help me with this. MTIA! :D


Use -LiteralPath instead of -Path because some of the special characters where interpreted with -Path.

Remove-Item -LiteralPath

Show-Command, Example 3: Open a cmdlet with specified parameters. This command opens a Show-Command window for the Connect-PSSession cmdlet. PowerShell. Copy. Hello (Still newish to scripting) I want to create a folder / path variable in Powershell and then run the command with some arguments, but Im failing


Special characters are just so much fun in PS right?

-LiteralPath works exactly like Patrick explained, but they don't allow for wildcards; because they're literal.

Have you tried using single quotes ' instead of double quotes ". This allows you to escape special characters, while still evaluating wildcards. Try the commands below:

New-Item -Path 'C:\Users\username\PSScripts\bracket`[\te$t.txt'
Get-Item -Path  'C:\Users\username\PSScripts\bracket`[\*'

Also, if it helps, I use VSCode for most scripting and, if you use tab completion, it will format this properly for you.

I hope that helps!

Exchange cmdlet syntax, Parameters that are available on the cmdlet. conventions, and also the syntax that's required to run commands in Exchange PowerShell. PowerShell.exe -Command Parameter Examples. The -Command parameter is the one of the most versatile PowerShell Parameters. The reason is simple. You can run other PowerShell commands with this parameter. You can also run scripts. The practical application of these these two scenarios will be demonstrated in the next section.


The solution to my overall problem ended up being this line of my batch script:

powershell -Command "Get-ChildItem -Recurse -LiteralPath %filePath% | Where-Object Extension -match '^(?!(\.avi|\.mkv|\.mp4)$)' | Remove-Item"

The regex was the hardest to get right, since I haven't used negative lookaheads before, but after ~2 hours of total time spent on this one line of code, it finally works!

Passing Parameters to a Script in an Executable File – SAPIEN Blog, When you run this script as a .ps1 file, you can use parameter names and values. PS C:\> .\New-WordTree.ps1 -Word PowerShell -Number 3� If you run this from within the PowerShell ISE by pressing F5, nothing interesting will happen. This is a case where you will need to run the saved file from the ISE Console and supply a value for the argument. To make sure PowerShell executes what you want, navigate in the command line to the same directory where you will save your scripts.


Understanding PowerShell Scriptblocks -- Microsoft Certified , It doesn't realize that Write-Host is a command to execute and the "Hi! Like functions, you can pass "parameters" to scriptblocks also known� Enter the script file path and any parameters. File must be the last parameter in the command, because all characters typed after the File parameter name are interpreted as the script file path followed by the script parameters. Typically, the switch parameters of a script are either included or omitted.


Passing parameters to and from the Run Powershell activity , Run a PowerShell script on the MID Server, or have the MID Server to run the script on a remote machine. Pass parameters to the PS script. Take� The most common use is to run the function or cmdlet as an elevated user account. For example, the cmdlet New-ADUser has a -Credential parameter, which you could provide domain admin credentials to in order to create an account in a domain. Assuming your normal account running the PowerShell session doesn’t have that access already.


[SOLVED] How to put variable in a powershell script?, Merge-MailboxFolder.ps1 -MergeFolderList $mergeYou'd run this fromt he In your example you appear to have a script that itself take three parameters. All the examples in this section use ShowArgs.exe with hypothetical parameters. I encourage you to run these examples so that you'll see the exact command-line parameters that PowerShell will use. Guideline 1. You only need to add quotes when you're specifying a parameter directly on the command line and that parameter contains spaces.