Can you redirect Tee-Object to standard out?

To complement zett42’s helpful answer: If you’re running PowerShell (Core) 7+, you can pass the file path that represents the terminal (console) to the (positionally implied) -FilePath parameter (in Windows PowerShell, this causes an error, unfortunately – see bottom section): # PowerShell 7+ only # Windows Get-Content data.txt | Tee-Object \\.\CON | data_processor.exe # Unix-like … Read more

Piping Text To An External Program Appends A Trailing Newline

tl;dr: When PowerShell pipes a string to an external program: It encodes it using the character encoding stored in the $OutputEncoding preference variable It invariably appends a trailing (platform-appropriate) newline. Therefore, the key is to avoid PowerShell’s pipeline in favor of the native shell’s, so as to prevent implicit addition of a trailing newline: If … Read more

How to implement using statement in powershell?

Here is a solution from Using-Object: PowerShell version of C#’s “using” statement which works by calling .Dispose() in a finally block: function Using-Object { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [AllowEmptyString()] [AllowEmptyCollection()] [AllowNull()] [Object] $InputObject, [Parameter(Mandatory = $true)] [scriptblock] $ScriptBlock ) try { . $ScriptBlock } finally { if ($null -ne $InputObject -and $InputObject -is … Read more

Is there a PowerShell equivalent of `paste` (i.e., horizontal file concatenation)? [duplicate]

A few months ago, I submitted a proposal for including a Join-Object cmdlet to the standard PowerShell equipment #14994. Besides complexer joins based on a related property, the idea is to also be able to do a side-by-side join (by omiting the -On parameter). Taken this Paste command in Linux as an example: $State=”Arunachal Pradesh”, … Read more

How can I elevate Powershell while keeping the current working directory AND maintain all parameters passed to the script?

Note: On 15 Nov 2021 a bug was fixed in the code below in order to make it work properly with advanced scripts – see this answer for details. The closest you can get to a robust, cross-platform self-elevating script solution that supports: both positional (unnamed) and named arguments while preserving type fidelity within the … Read more

Getting “System.Collections.Generic.List`1[System.String]” in CSV File export when data is okay on screen

If an object you export as CSV with Export-Csv or ConvertTo-Csv has property values that contain a collection (array) of values, these values are stringified via their .ToString() method, which results in an unhelpful representation, as in the case of your array-valued .IPV4Addresses property. To demonstrate this with the ConvertTo-Csv cmdlet (which works analogously to … Read more