As for this,
"$cat ExampleFile.txt"
$cat or "$cat" specified this way is a variable.
"$cat D:\temp\TestFile.txt"
# Results
<#
"$cat D:\temp\TestFile.txt"
The variable '$cat' cannot be retrieved because it has not been set.
At line:1 char:2
+ "$cat D:\temp\TestFile.txt"
+ ~~~~
+ CategoryInfo : InvalidOperation: (cat:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined
#>
PS will evaluate that to whatever is the value/data in the $cat
variable, not run the cat
command.
See the available info on PowerShell variable expansion.
https://devblogs.microsoft.com/powershell/variable-expansion-in-strings-and-here-strings/
As noted cat
is an alias for Get-Content
, and it must be used that way.
For the command to work, you must do it this way.
cat 'D:\temp\TestFile.txt'
# Results
<#
1 21/05/20
2 21/05/20
3 21/05/20
4 21/05/20
5 21/05/20
6 21/05/20
7 21/05/20
8 21/05/20
#>
Unless you did this...
$cat = cat
... somewhere else in your code. Meaning you assigned a command to a variable name. Which, especially for a single comment is not really prudent.
If you tried that in this use case, it would still fail.
$cat = cat
"$cat D:\temp\TestFile.txt"
# Results
<#
$cat = cat
"$cat D:\temp\TestFile.txt"
cmdlet Get-Content at command pipeline position 1
Supply values for the following parameters:
Path[0]:
#>
To get that variable stuff to work in the above, you have to declare it either as a function or a ScriptBlock. IMHO just overcomplicating things for something the direct if you did.
# using a ScriptBlock
$cat = {cat $($args[0])}
& $cat 'D:\temp\TestFile.txt'
# Results
<#
1 21/05/20
2 21/05/20
3 21/05/20
4 21/05/20
5 21/05/20
6 21/05/20
7 21/05/20
8 21/05/20
#>
about_Script_Blocks
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_script_blocks?view=powershell-7.3
Lastly for any external command to be discoverable, it must be in your system and PS paths.
See also PS command precedence
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_command_precedence?view=powershell-7.3
PowerShell- Running Executables - TechNet Articles - United States
(English) - TechNet Wiki
https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
cat
is an internal command alias forGet-Contnet`. Using this,
$cat``` is a variable name that has to have an assigned value. You can get a list of all PS aliases by usingGet-Alias -Name '*' | Out-Gridview
.