How to assign file content into variable as object

Trần Ngọc Nam 20 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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,406 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,136 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,111 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