It’s gonna be hard to give reasonable advices how to deal with the input data when you don’t have a clue what the input data looks like. What I can tell from overviewing your code:
Please format your code as code - you may read the first pinned post on top of the list of posts in this forum recommending to read it before posting: Read Me Before Posting! You’ll be Glad You Did!
Please do not use aliasses. They make the code harder to read.
Please use format cmdlets like Format-Table always as the last cmdlet in a pipeline.
If you like to have CSV file you should name it as such and you should use descibing names vor file names and variables.