Ash
Ash

Reputation: 41

Powershell: pass system key as a argument

I want to pass -Unique as parameter for select-string

param(
    $y="CAR"
    $parameter=""
)
get-childitem -r -i "*.txt"| select-string "TABLE FILE $y" | Select-Object filename,path $parameter

But whenever i try to pass the parameter, i get the following error

powershell : Select-Object : A positional parameter cannot be found that accepts argument '-Unique'.
At line:1 char:1
+ powershell -ExecutionPolicy ByPass -File x.ps1 -parameter '-Unique'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Select-Object :...ment '-Unique'.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

At C:\Users\aravikumar\Downloads\x.ps1:5 char:62
+ ... elect-string "TABLE FILE $y" | Select-Object filename,path $parameter
+                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Select-Object], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

Also want to pass multiple system key if possible such as -unique -simplesearch -casesensitive

Upvotes: 0

Views: 180

Answers (1)

Burt_Harris
Burt_Harris

Reputation: 6864

You can't pass a switch as a string parameter like that. You need to use a technique called 'splatting'. See help about_splatting or https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/about/about_splatting

Upvotes: 1

Related Questions