Forum Discussion
Get the output of a command into a string
- Sep 23, 2021
Hello, write the output of first step to external file, examplae text.csv
with these file you can read each line and add the variable with content of the file.Get-Content .\test.csv | ForEach-Object { if($_ -match $temp){ #I add write-output to show the content of the file test.csv[n] Write-Output $_ } }
Hi, thanks, that is great!
So now I got the output in a text file, with this structure:
SamAccountName
--------------
User1
User2
User3
So I can make the next step I need to clean up the first 2 lines of the output text file, so I can have the needed values to compare only.
I've searched and tried to use this command:
Get-Content $Path | Select-Object -Skip 3 | Set-Content $Path
If I run this on the file without the Set-Content $Path it works just fine, but when goes to write to the output file, I always got the error:
Set-Content : The process cannot access the file 'C:\Temp\teste.txt' because it is being used by another process.
I've read about it, and some say it's related to having Powershell ISE opened, but I close it and it's just the same.
Any tips on this?
Thanks
- dmarquesgnSep 23, 2021Iron ContributorWell, at least it solved the issue 🙂
Thanks- yuzoyoxSep 23, 2021Iron ContributorPlease, do not forget to select as a best solution for your question.
Thank you very much- dmarquesgnSep 24, 2021Iron Contributor
Hi,
I still have an issue that found out later.
When I create the first file to store the SamAccountName for each user, if I use the option Out-File <file.txt> or <file.csv>, I have a lot of blank spaces after the name, so when I input that line by line into the Get-ADPrincipalGroupMembership, it won't work, because the name doesn't match due to the spaces.If I use the option Export-Csv, then the name has the char "name", so it won't be readable again on the next step.
Is there any easy way to remove the extra spaces on the txt file or the "" on the csv file?
Thanks