Sander Rozemuller
Sander Rozemuller

Reputation: 13

Add character end of line in PowerShell

I'm looking for a solution for the following challenge:

I run the following command in PowerShell.

Import-Module servermanager
Get-WindowsFeature | where {$_.Installed -eq "True"} | ft DisplayName, Installed > output.txt

Now I want to add a character at the end of each row. How can I do that?

I think I have to add the content into an array, but I don't know how to finish the code.

At the end I have to load the content into the EventViewer. If I send it directly, the event description isn't formatted well.

Upvotes: 1

Views: 4141

Answers (3)

schelljw
schelljw

Reputation: 21

It's a little outside the scope of what you directly asked, but I'd suggest skipping the 'write to text file' stage and pass directly to the destination.

Import-Module servermanager
$installedFeatures = @()    # Creates a blank array for the features
$output = @()    # Creates a blank array for formatted results

$yourChar    # The character/ string you want to add at the end of each row

$installedFeatures = Get-WindowsFeature | Where-Object {$_.Installed -eq "True"}
foreach($feature in $installedFeatures)
{
    $output += "$($feature.displayName) $($feature.installed) $yourChar"
}

Once you've iterated through all the Windows features, your $output variable will have an array of strings in the format of displayName installed $yourChar. You can then write to disk, or send the object somewhere else (this is the beauty of PowerShell objects!).

Upvotes: 0

pobrelkey
pobrelkey

Reputation: 5973

It sounds like instead of using ft > output.txt, you want something like:

foreach { echo ( $_.DisplayName + " " + $_.Installed + " extra_stuff" ) } > output.txt

It doesn't give you a nicely formatted table though.

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200213

You could add another field to the records like this:

Get-WindowsFeature | ? { $_.Installed } |
    select DisplayName, Installed, @{n='Other',e={'c'}} | ft

Upvotes: 2

Related Questions