How to assign file content into variable as object

Trần Ngọc Nam 35 Reputation points
2023-04-24T10:03:53.3866667+00:00

I'm having problems like: First, I use

$localGroupMembers = Get-LocalGroupMember -Group $groupName -ErrorAction SilentlyContinue | Where-Object {$_.PrincipalSource -eq "Local"}

to get members in group and export file with

$localGroupMembers | Out-File -FilePath $reportFile -Append

And then, I want get it again with

$localGroupMembers2 = Get-Content $file

But localGroupMembers2 is not object such as localGroupMembers. So, is there a way to convert localGroupMembers2 to the same format as localGroupMembers?

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,906 Reputation points
    2023-04-24T15:07:59.92+00:00

    Use the Export-CliXml cmdlet instead of Out-File. Then use the Import-CliXml cmdlet to recreate the object.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.