Normally, PowerShell commands can be chained with semicolons. The following opens 2 notepads:
PS> notepad; notepad
You can also chain a more complex statement:
PS> Add-Type -AssemblyName System.IO.Compression; `
> $src = "C:\aFolder"; $zip="C:\my.zip"; `
> [io.compression.zipfile]::CreateFromDirectory($src, $zip)
Chained PowerShell commands can also be called from a CMD command-line:
C:\> powershell notepad; notepad
This post describes a method to create a .Net 4.0 PowerShell prompt, even if .Net 2.0 is the active framework on your OS. You create a .cmd script, and run that. Now you're in a .Net 4.0 environment.
Chaining also works at that 4.0 prompt:
C:\> ps4.cmd
PS> notepad; notepad
And also works from standard CMD prompt:
C:\> ps4 notepad; notepad
This post describes a way to do Add-Type
to an explicit pathname (needed to reference 4.0 assemblies from the ps4 prompt):
Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.IO.Compression.FileSystem\v4.0_4.0.0.0__b77a5c561934e089\System.IO.Compression.FileSystem.dll"
That works, even when chained at the ps4 prompt:
C:\> ps4
PS> Add-Type -Path "C:\x\System.IO.Compression.FileSystem.dll"; `
> $src = "C:\x\xl"; $zip="C:\x\xl.zip"; `
> [io.compression.zipfile]::CreateFromDirectory($src, $zip)
Problem: chaining the above statement fails when ps4 is launched at a standard command prompt (error in full at bottom of post):
C:\> ps4 Add-Type -Path "C:\x\System.IO.Compression.FileSystem.dll"; $src = "C:\x\xl"; $zip="C:\x\xl.zip"; [io.compression.zipfile]::CreateFromDirectory($src, $zip)
Yet, all the above methods work. Why? How can I make this work?
The term 'C:\x\xl' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:73 + Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll; $src = C:\x\xl <<<< ; $zip=C:\x\xl.zip; [io.compression.zipfile]::CreateFromDirectory($src, $zip) + CategoryInfo : ObjectNotFound: (C:\x\xl:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException The term 'C:\x\xl.zip' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:91 + Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll; $src = C:\x\xl; $zip=C:\x\xl.zip <<<< ; [io.compression.zipfile]::CreateFromDirectory($src, $zip) + CategoryInfo : ObjectNotFound: (C:\x\xl.zip:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Exception calling "CreateFromDirectory" with "2" argument(s): "The path is not of a legal form." At line:1 char:138 + Add-Type -Path C:\x\System.IO.Compression.FileSystem.dll; $src = C:\x\xl; $zip=C:\x\xl.zip; [io.compression.zipfile]::CreateFromDirectory <<<< ($src, $zip) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException