From fc9b23f3571f8b3f67c83ad7814ec3c579ece22e Mon Sep 17 00:00:00 2001 From: Andrew Ryan Davis Date: Thu, 20 Aug 2020 00:10:08 -0700 Subject: [PATCH] Adding in escape chars and more info on call operator with exe's escape chars like newlines, tabs, negating affects call operator and dot-sourcing for paths to exes with spaces in them and arguments passed. --- powershell.html.markdown | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/powershell.html.markdown b/powershell.html.markdown index da4dcd5c..027320d5 100644 --- a/powershell.html.markdown +++ b/powershell.html.markdown @@ -677,6 +677,19 @@ $PSVersionTable ```Powershell # Calling external commands, executables, and functions with the call operator. +# Executables with arguments passed create issues +C:\Program Files\dotnet\dotnet.exe +The term 'C:\Program' is not recognized as a name of a cmdlet, function, script file, or executable program. +Check the spelling of the name, or if a path was included, verify that the path is correct and try again + +"C:\Program Files\dotnet\dotnet.exe" +C:\Program Files\dotnet\dotnet.exe # returns the string rather than execute it + +&"C:\Program Files\dotnet\dotnet.exe --help" # fail +&"C:\Program Files\dotnet\dotnet.exe" --help # success +# Alternatively, you can use dot-sourcing here +."C:\Program Files\dotnet\dotnet.exe" --help # success + # the call operator (&) is similar to Invoke-Expression, but IEX runs in current scope. # Standard usage of '&' would be to invoke a scriptblock inside of your script. # Notice the variables are scoped @@ -693,7 +706,20 @@ $i # => 5 $x=1 &{$x=2};$x # => 1 -. {$x=2};$x # => 2 +.{$x=2};$x # => 2 + + +# Escape Characters in Powershell +# Many languages use the '\', but Windows uses this character for +# file paths. Powershell thus uses '`' to escape characters +# Take caution when working with files, as '`' is a +# valid character in NTFS filenames. +"Showing`nEscape Chars" # => new line between Showing and Escape +"Making`tTables`tWith`tTabs" # +> Format things with tabs + +# Negate pound sign to prevent comment +# Note that the function of '#' is removed, but '#" is still present +`#Get-Process # => Fail: not a recognized cmdlet # Remoting into computers is easy