Skip to main content
added 216 characters in body
Source Link
postanote
  • 4.9k
  • 2
  • 8
  • 7
added 623 characters in body
Source Link
postanote
  • 4.9k
  • 2
  • 8
  • 7

PS will evaluate that to whatever is the value/data in the $cat variable, not run the catcat command.

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.

PS will evaluate that to whatever is the value/data in the $cat variable, not run the cat command.

Lastly for any command to be discoverable, it must be in your system and PS paths.

PS will evaluate that to whatever is the value/data in the $cat variable, not run the cat command.

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.

added 882 characters in body
Source Link
postanote
  • 4.9k
  • 2
  • 8
  • 7

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 ExampleFile'D:\temp\TestFile.txttxt'
# 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]:
#>

Lastly for any 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

As for this,

"$cat ExampleFile.txt"

$cat or "$cat" specified this way is a variable.

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 ExampleFile.txt

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.

Lastly for any 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

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]:
#>

Lastly for any 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

Source Link
postanote
  • 4.9k
  • 2
  • 8
  • 7
Loading