String Manipulation to CSV field

Hi,

I have created my own script to help me organize software.
I have it pull the serial number, name of software from a file using get-content.
I then export it to a CSV file, it all works fine. However I just want to clean up the formatting on it.
For example on the Software field, it populates software:Photoshop, I want the cell in the CSV file to only show Photoshop not the word software. The file its getting its content from is just a text file with two different lines. Look like this
Software: Photoshop
Serial: 111-111-111

It then exports it to a csv file that has 4 column headers, Serial, Software, PCName and Date. The information gets populated to the correct column. The issue is under software and serial, it does list the software and serial but in front of software name says software:photoshop. I want it to only record photoshop under software column.
This is my code. Any help would be appreciated.

I apologize if I’m missing code, items but this is to the best of my abilities, I have read other articles before posting here. I’m a beginner as well, I have been doing this for a couple of months but only as of recently have I started getting serious about it.
Any help would be appreciated.

New-Object -TypeName PSCustomObject -Property @{
Serial = $Serial
Software = $SoftwareName
PCName = $PCname
Date = $DateTime
} | Export-Csv -Path C:\temp\software.csv -NoTypeInformation -Append

$Path = "C:\temp\mail.txt"
$Serial = Get-Content $Path | Where-Object {$_ -like '*Serial Number*'} 
$Serial.Substring($Serial.Length-14)
$SoftwareName = Get-Content $Path| Where-Object {$_ -like '*Software*'} 
$SoftwareName.Substring($SoftwareName.Length-10) 
$PCname= hostname 
$DateTime =Get-Date -Format D

jasieltego, welcome back to the forums.

Your code seems missformatted and is missing some charachters. I assume it’s because you missed to fomrat it as code. Please correct this by editting your question and using the </> symbol in the format bar.

Thanks in advance.

It’s hard to recommend something meaningful without knowing the actual format of your source files where you get all the information from. But when the string you get from it always looks the way I assume from your description you could use a simply -split with a .trim() to remove the unwanted charachters. … something like this for example:

(' software : Photoshop ' -split ':')[1].Trim()

If you could share a few sanitzed lines of your input files we mught be able to help a little further. This could make your code a little more robust and reliable. :wink:
If you share sample data please format this as code as well. Here you can read how that works: Guide to Posting Code.

Hi Olaf,

Thank you. I have edited my question as suggested and pasted the code in the format you asked.
Thanks for the help.

I have used the trim function on the string and I still get the same results. Here is the interesting part when I use the code below when the script runs in ISE it displays correct as 111-111-1111 For serial number column and Photoshop for Software Column. on the exported CSV file it shows up incorrect.

$Serial = Get-Content $Path | Where-Object {$_ -like '*Serial Number*'} 
$Serial.Trim("Serial Number: ")

The .trim() method does not work as you probably think it does. I’ll try to make it more obvious:
The .trim() method of the string class removes ALL consecutive charachters you specify from the beginning and the end of a string - not words - consecutive charachters. See the following code snippet:

'Software: Photoshop'.trim('erwa:oStf')
   'Software: VMware'.trim('erwa:oStf')
'Serial: 111-111-111'.trim(':rlSaei')
'Serial: 111-111-111'.trim('-1')

And BTW - it’s case sensitive! :point_up_2:

Anyway - my suggestion was something else:

I recommended using a -split AND a .trim(). The -split to remove the part before the colon and the .trim() to remove the remaining spaces.

And I’d like to repeat myself again:

I could imagine that there could be a better way to achieve what you’re looking for. :wink:

Thanks Olaf, I tried your suggestion , alternatively I tried trim as well. I can’t get it to work.
Thing is why does it work in ISE, it shows it correct but on my csv file it does not. I told you exactly what my source file has as well, just two lines, software name and serial number.

the trim function with words worked for me in Powershell ISE as well. Your right,. I don’t see too much documentation on that , mainly on personal blogs.
Thanks for all your help, this is something I’m going to need to figure out by myself

If all your files really have this format you put way too much effort into this task.

Try this:

$Path = 'C:\temp\mail.txt'
Import-Csv -Path $Path -Delimiter ':'

This should give you the columns Software and Serial with their according values.

Hi Olaf,

Thank you for all your persistent help and patience. Unfortunately -Delimeter suggestion did not work either. However I was able to fix it. I declared another variable, that reads the original variable after it has gotten the context from the text file. I then use Trim to trim Serial Number word from it.

btw I used the trim function by typing in the word in double quotes and it works. May not be the recommended way but it did work.

$Serial = Get-Content $Path | Where-Object {$_ -like '*Serial Number*'} 
$Serial1 = $Serial.Trim("Serial Number:")

After this I went back to my object and change the Variable name from $Serial to $Serial1 to read the string after it’s trimmed. My CSV is working perfect now.

Thanks again

That’s not helpful at all. How did it not work? Did you get errors? If yes what error?

It does not work like you (still) think it does. Please read my answers about this topic again and try to understand the code I posted. If it would work like you think you could remove one single number from the end of a string with something like this:

'Serial: 111-111111'.trim('1')

Try to answer for yourself: How many “1” are provided in the .trim() method and how many numbers got removed from the string and why!? :thinking:
It may work now but when the conditions change the behaviour of your script may be unreliable or unpredictable.

Hi Olaf,

It sounds like your coming of as defensive, if so , I’m not questioning your knowledge of PowerShell, I know your universes smarter than me in this subject. You told me it does not work with Words, when I told you it worked for me it was not intended as a jab or to prove you wrong. It was more of a it worked, I don’t know why but it did . You are absolutely correct about the trim method, I see your examples and your correct, I’m just simply saying it worked for me, I got the desired effect of what I was trying to achieve.

What I meant by it did not work is, it ran successfully, it does not show any errors at all. However when I check the CSV file it still shows the word Software in front of the name of the software.

Once again I apologize if I irritated you or made it sound like I’m questioning your knowledge.
Like I said, I’m new to powershell, I’m reading on it everyday and yes, I will make mistakes, and lots of them and I just have to learn from them.

Perhaps I made the corrections you suggested in the wrong spot. However, I wonder if you ran the code I sent and if you tried to recreate the problem to see if you got the same issues as me.
Either way, I want to thank you for educating me.
I have found a solution that works for me.

No worries - you didn’t. :wink: I just would like you to understand correctly the way it works to be able to reproduce the right behaviour if you need it.

That’s great, I’m glad for you. :wink:

1 Like

As you continue learning PowerShell, you should be aware that the ISE is not a reliable test environment. Development ended in 2017 along with Windows PowerShell v.5.1. If you are using an up-to-date Windows platform, you should actually be using PowerShell Core v.7 (to check this just echo $PSVersionTable).

The execution console in PowerShell ISE is actually not a ‘real’ console, but an emulation of Windows PowerShell running inside of a Win32 application, and it was never a completely accurate emulation.

When testing your scripts, make sure that you test them against the version of PowerShell that you are actually using. VSCode is the recommended development tool.