write-host -NoNewline parameter is ignored with powershell 5.1 - powershell

Starting with WMF 5.1 the -NoNewLine option for Write-Host is essentially ignored.
Simple example:
PS C:\> Write-Host "no newline test " -NoNewline
Current Behaviour:
no newline test
PS C:\>
Expected Behavior
no newline test PS C:\>
PS version Table is as follows :
Name Value
---- -----
PSVersion 5.1.14409.1012
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1012
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
As per Microsoft docs for powershell 5.1, Next Prompt should come in the same line after output of Write-Host "no newline test" -NoNewline statement.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host?view=powershell-5.1
Any idea why it is not functioning as expected?

Related

Export-Counter missing from Powershell 7.1

Any idea how to export counters in Powershell 7.1.0? The command is missing with the latest update.
Get-Command -Module Microsoft.PowerShell.Diagnostics
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Get-Counter 7.0.0.0 Microsoft.PowerShell.Diagnostics
Cmdlet Get-WinEvent 7.0.0.0 Microsoft.PowerShell.Diagnostics
Cmdlet New-WinEvent 7.0.0.0 Microsoft.PowerShell.Diagnostics
❯ $PSVersionTable
Name Value
---- -----
PSVersion 7.1.0
PSEdition Core
GitCommitId 7.1.0
OS Microsoft Windows 10.0.19042
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0

Unable to Unzip Folder when invoked on a remote PC

I'm Attempting to Unzip A folder located on a shared drive to the root of C:\ on a remote PC but keep getting errors, how do i correct this Powershell?
$Computers = "LN-T48-PF11BL57"
Invoke-Command -Computername $Computers -ScriptBlock {
Expand-Archive -LiteralPath '\\LNAPPS\APPS\Adobe iManage Fix\Program Files (x86).zip' -DestinationPath C:\ -Force } -Verbose
Write-Host "Enter to Exit"
This returns the following error:
A positional parameter cannot be found that accepts argument '\\LNAPPS\APPS\Adobe iManage Fix\Program Files (x86).zip'.
+ CategoryInfo : InvalidArgument: (:) [Expand-Archive], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Expand-Archive
+ PSComputerName : LN-T48-PF11BL57
A few things here:
Remember Windows balks are dropping stuff in the root c:.
So, are we to assume that \LNApps is a server name and \APPS is a
folder share configured on that server?
Lastly, unless that server is running PowerShell v5, that
Expand-Archive cmdlet is not there.
Hence this...
: InvalidArgument: (:) [Expand-Archive], ParameterBindingException
(Get-CimInstance -ClassName CIM_OperatingSystem).Caption
Microsoft Windows Server 2012 R2 Standard
$PSVersionTable
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.42000
BuildVersion 6.3.9600.19170
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
Get-Command -Name '*Expand-archive*'
# No results
(Get-CimInstance -ClassName CIM_OperatingSystem).Caption
Microsoft Windows 10 Pro
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.17763.316
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.17763.316}
BuildVersion 10.0.17763.316
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Get-Command -Name '*Expand-archive*'
CommandType Name Version Source
----------- ---- ------- ------
Function Expand-Archive 1.0.1.0 Microsoft.PowerShell.Archive
If that cmdlet is not there, you need to use the .Net namespace, System.IO.Compression.FileSystem, to deal with this use case.
can be used to either compress or extract files using this class. The
following example will compress the files stored in the c:\testing
folder:
Add-Type -Assembly 'System.IO.Compression.FileSystem'
[System.IO.Compression.ZipFile]::CreateFromDirectory('c:\testing', 'c:\testing.zip','Optimal',$false)
When you want to extract files, use the ExtractToDirectory method:
[System.IO.Compression.ZipFile]::ExtractToDirectory('c:\testing.zip', 'c:\newtest')

Get-Command not behaving as described

All the examples I see for Get-Command show the results as
CommandType Name Definition
----------- ---- ----------
Whereas I get:
CommandType Name Version Source
----------- ---- ------- ------
I am using Windows 7 with Powershell v5, PSCX, more details below:
PS C:\Users\pollock> $PSVersionTable
Name Value
---- -----
PSVersion 5.0.10586.117
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.10586.117
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Any ideas why?
Thats probably because the examples omit the Source column. However, you can use the Select-Object cmdlet to select the properties you want:
Get-Command | Select-Object CommandType, Name, Definition

Psedit on a item created with the New-Item cmdlet throws exception

When I create a new file using the New-Item cmdlet and want to edit it within PowerShell ISE using psedit I receive the following exception:
Exception calling "Add" with "1" argument(s): "Object reference not set to an instance of an object."
At line:7 char:13
+ $psISE.CurrentPowerShellTab.Files.Add($_.FullName) > $nul ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : NullReferenceException
Code:
New-Item readme.txt
psedit readme.txt
PsVersionTable:
Name Value
---- -----
PSVersion 5.0.10586.494
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.10586.494
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Is this a known Bug? Any workaround?
Running the following works without an issue and opens a new tab in the ISE with the readme.txt:
New-Item Readme.txt -ItemType file
psedit Readme.txt
PSVersiontable:
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.34209
BuildVersion 6.3.9600.17400
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
It seems like this is a PowerShell V5 bug since its working fine on PowerShell V4 (see the comment from Micky Balladelli). A workaround is to create the file using the Out-File cmdlet:
'' | Out-File readme.txt
psedit readme.txt

powershell remote import module error

I have a problem with this code
Invoke-Command -ComputerName $CSC_SERVER -ScriptBlock {
Import-Module ("C:\Build_v.6.1.0.9\Tools\CommonComponents.BuildCommands.dll")
}
Error:
Could not load file or assembly "file: /// C: \ Build V.6.1.0.9 \ Tools \ CommonComponents.BuildCommands.dll" or one of their dependent components. The assembly created in a later version of the runtime than the current, and can not be loaded.
When I go to the server via RDP and try to load this module everything is OK.
how to fix this problem?
UPD
> Invoke-Command -ComputerName $CSC_SERVER -ScriptBlock {$PSVersionTable}
Name Value
---- -----
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
PSCompatibleVersions {1.0, 2.0}
CLRVersion 2.0.50727.5420
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
And same via local session on remove server
> $PSVersionTable
Name Value
---- -----
PSVersion 2.0
PSCompatibleVersions {1.0, 2.0}
BuildVersion 6.1.7601.17514
CLRVersion 4.0.30319.17929
WSManStackVersion 2.0
PSRemotingProtocolVersion 2.1
SerializationVersion 1.1.0.1
Now I see that difference in CLRVersion. Powershell remoting uses CLR version 2.0. What is the reason for this behavior? And how to change it to version 4.0?