Why You Shouldn’t Use $input In PowerShell

Mark Harwood
1 min readJul 29, 2021

Just a short post and a gentle reminder to check conventions before pulling my hair out over a simple issue.

This all started when I wanted to test something simple in PowerShell. I had started using C# more often and I think this is what caused my brain just to use $input as I would normally use something like this in C#.

In C# my test functions (or methods) might look something like this:

public string TestFunction(string input){ 
Console.WriteLine(input);
}

In C#, the above works…

However, $input is a reserved variable in PowerShell. You can see a list of them all here. But much to my annoyance, the PowerShell ISE doesn’t flag this as an issue (technically it isn’t) and just runs like nothing is wrong (once again, technically nothing is wrong).

In PowerShell, this doesn’t work…

function Test-Function([string]$input){ 
Write-Host $input
}

After messing for about thirty minutes, I finally found this knowledge out and renamed my parameter. I think you’ll like the name I continued to use… 😊

function Test-Function([string]$stupidFkingInput){ 
Write-Host $stupidFkingInput
}

Enjoy!

--

--

Mark Harwood

IT Pro 👨‍💻 Dad 👀 Husband🔒 Check out my free blog or utilise my expertise on the web! https://mharwood.uk