This will be easy for most of you answer, but I'm struggling with it.
For instance, if I input Get-Tpm, the return I get is,
Get-tpm written to console, as shown below.
If I enter
ECHO Get-tpm > C:\dssupport\output.txt
the output is a text file containing 'Get-tpm:'
Is there a way to do both, so that I can see it on my monitor,
and also saved as a text file?
I'm grateful for any help at all.
Thank you
Most PowerShell cmdlets like Get-Tpm output objects which usually display on the console screen formatted as table or as list.
You can easily write output like that to a file, usually a structured Csv file you can open in Excel using Export-Csv.
That last cmdlet however does not have something neat like a parameter called -PassThru, but what you can do is use a cmdlet that does let you do that.
In your example, you could do
# show on screen AND write to CSV file
Get-Tpm | ConvertTo-Csv -NoTypeInformation | Set-Content -Path 'X:\TPM.csv' -PassThru
or
# show on screen AND write to JSON file
Get-Tpm | ConvertTo-Json | Set-Content -Path 'X:\TPM.json' -PassThru
or even
# show on screen as list AND write to text file
Get-Tpm | Format-List | Out-String | Set-Content -Path 'X:\TPM.txt' -PassThru
Although a file written like that is not really meant to be used for anything other than display purposes.
Or you can do this in separate lines of code where you show on screen as list for instance and in the other line write the csv file.
For that, you need to capture the results from the cmdlet in a variable:
$result = Get-Tpm
# show on screen as list (or table)
$result | Format-List # or do Format-Table -AutoSize
# show in a separate grid view window
$result | Out-GridView -Title "My TPM results"
# output to CSV file
$result | Export-Csv -Path 'X:\TPM.csv' -NoTypeInformation
Related
im trying to save the output from a command to a variable,
but i just cant get it to work.
It should look something like this :
$test = (Get-SmbShare | Select Name,CurrentUsers,CurrentUserLimit | fl)
Write-Host $test
Output: The Output of Get-SmbShare
I was looking through the comments and I saw you said that you want to store it in a text file.
#Lee_Dailey Thats what i want to do, the output is gonna go into a text file. – Jan
So if you want to skip the variable part and just send it straight to a text file, you could use:
Get-SmbShare | Select Name, CurrentUsers, CurrentUserLimit | fl | Out-File -filepath "C:\Temp\file.txt"
I am using get-winevent to convert an evtx log to .json file. Then I've send it to ELK. Get-WinEvent -Path .\log.evtx | ConvertTo-Json|Format-List | Out-File log.json
The file looks like a normal string containing file on windows. But when I take it to linux, it contains binary data and cannot be parsed to ELK.
Even if I use out-string, nothing changes. $result = Get-WinEvent -Path .\user-creation-1log.evtx | ConvertTo-Json| Format-List
$result | Out-String | out-file log.jsonThis also appears like a binary file in linux. (Although I remember export-csv with get-winevent created complete text file, but this makes a really ugly formatted csv file). I really liked the way convertTo-json formatted and valued the json data and would prefer it. (if someone can provide a different way to convert the evtx data in its fullest form to json, happy to take).
I've tried evtx2csv python module, but that doesn't write output to a file.
First, don't use Format-List if you intend to export JSON. This is only for formatting objects as a nice visual representation in the console.
Also, I don't use Linux, but I guess it's safest to specify utf8 as encoding explicitly to make sure it's compatible:
Get-WinEvent -Path .\log.evtx | ConvertTo-Json | Out-File log.json -Encoding utf8
I recently started using Powershell and I'm trying out some code.
I have a .cfg file with several rules of code. The code is written like this:
ad.name=1
ad.virtual=active
ad.set=none
ad.partition=78
Now I want to export the value of ad.partition, which is 78, to a new file. I don't want to export ad.partition or = but only the number 78.
So far I got this:
Get-Content -Path C:\file.cfg | Where-Object {$_ -like 'ad.partition=78'}
But then I -obviously- just get the variable and the value. Not sure how to continue...
I hope someone has a way of achieving what I want.
After saving the value in a new file, would it be possible to add spaces? For example, the value consists out of 9 digits, e.g. 123456789. The desired output result would be 123 456 789.
Use ConvertFrom-StringData cmdlet to create a hash table from your file, then simply index the key you are after:
$h=(Get-Content -Path C:\file.cfg | ConvertFrom-StringData)
$h.("ad.partition") -replace ('^(\d{1,3})(\d{1,3})?(\d{1,3})?','$1 $2 $3') > C:\out.cfg
You can use the Select-String cmdlet to capture your desired value using a regex. Then just pipe the result to the Out-File cmdlet. To get your desired output with spaces, you can use a simple format string:
"{0:### ### ###}" -f [int](Select-string 'ad\.partition=(.*)' -Path C:\file.cfg).Matches.Groups[1].Value |
Out-File C:\result.cfg
I have two csv files i need to compare them, then output a new file that will have the new values of the prophetess that changes and will mark(highlight) those properties some how. I already have the resulting file with all the values and all the properties but i dont know how to mark the specific properties that changed. Is there a way to do that is powershell?
Here is a sample of my code:
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
$file1,
[Parameter(Mandatory=$true)]
$file2
)
$content1 = Import-Csv -Path $file1
$content2 = Import-Csv -Path $file2
$props = $content1 | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
$comparedLines = Compare-Object $content1 $content2 -Property $props -PassThru | Where-Object {$_.SideIndicator -eq "=>" }
$comparedLines | Export-csv -Path C:\FullPUF\Difference.csv –NoTypeInformation
So, if I understand this correctly, $content2 is the newer file, right?
If so, then $comparedlines is going to contain all of the values which changed. If that's right, then we're in business, because Doug Finke wrote an absolutely awesome PowerShell module called importExcel that is going to make this trivially easy. But first, how did I do this?
I needed some files to test against, and I didn't have your source file so I couldn't guess at the values. I just made a simple CSV with a Name and Position value, and changed the Position numbers between Content1 and Content2.
How to solve this problem, first off, download Import-Excel right now. If you're on PowerShell version 4.0 or higher you can install it from PowerShell like so:
Find-Module ImportExcel | Install-Module
This module has the concept of ConditionalText filter rules. You can create a new one like this.
1..5 | Export-Excel C:\temp\test.xslx -Show`
-ConditionalText (New-ConditionalText -ConditionType GreaterThan 3)
This will output and display a new spreadsheet with the numbers 1 through 5 in it, and highlight the ones higher than 3.
We can also highlight rows that ContainsText which matches a value we know.
So, we take the value of $comparedLines using ForEach-Object, and create a New-ConditionalText rule to highlight the line if it contains one of the properties.
$highlight =$comparedLines | % {New-ConditionalText -ConditionalType ContainsText $_.Name}
Next, we will echo the contents of $content2, and use Export-Excel to create a new .xslx file and apply a conditional formatting rule to it.
$content2 | Export-Excel r:\test.xlsx -show -ConditionalText ($highlight)
And the output
You'll probably need to play with the code a tiny bit, and modify it to fit whatever your columns are called, but this is how you can export a csv and highlight the differences using PowerShell. Except there's no such thing as highlighting or text format in a .csv so you have to use an Excel SpreadSheet instead.
Lemme know if you need me to dig deeper anywhere :)
i hope someone can help.
I am trying to manipulate a file created by powershell.
I managed to get to the end result that i want, but i am sure it would be easier if it was only one command.
# Invoke the Exchange Snapping ( make sure you are Exchange Admin to do it SO)
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010
#Create a file with list of DL in the organization
Get-DistributionGroup | Select-Object Name | Out-File C:\Pre_DLGroups.txt
$content = Get-Content C:\Pre_DLGroups.txt
#Remove the 3 first lines of the file that you dont need it
$content | Select-Object -Skip 3 | Out-file C:\DLGroups.txt
#Trim the space in the end and crate the Final file
Get-Content C:\DLGroups.txt | Foreach {$_.TrimEnd()} | Set-Content c:\FinalDLGroup.txt
is that way to make the end result in a single file rather than creating 3?
cheers
Elton
You can send your content across the pipeline without writing it out to files. You can use parenthesis to group the output of certain sets of cmdlets and/or functions, and then pipe that output through to the intended cmdlets.
This can all be applied on a single line, but I've written it here on multiple lines for formatting reasons. The addition of Out-String is something of a safety measure to ensure that whatever output you're intending to trim can actually be trimmed.
Since we're not getting this content from a text file anymore, powershell could possibly return an object that doesn't understand TrimEnd(), so we need to be ready for that.
(Get-DistributionGroup | Select-Object Name) |
Out-String |
Select-Object -Skip 3 |
Foreach {$_.TrimEnd()} |
Set-Content c:\FinalDLGroup.txt
However, an even smaller solution would involve just pulling each name and manipulating it directly. I'm using % here as an alias for Foreach-Object. This example uses Get-ChildItem, where I have some files named test in my current directory:
(Get-ChildItem test*) |
% { $_.Name.TrimEnd() } |
Set-Content c:\output.txt
Get-DistributionGroup |
Select-Object -ExpandProperty Name -Skip 3 |
Set-Content c:\FinalDLGroup.txt