How to Split values and export in CSV

Mike 246 Reputation points
2022-09-14T13:14:01.87+00:00

I am currently trying to export an information but I am having problems with the ObjectGUID. It has a value "7a42fc27-02e1-4a82-a2fd-b27ebbd382d2" but I wanted to remove "-" and join them as one string "7a42fc2702e14a82a2fdb27ebbd382d2"

$devices = get-adcomputer -server domain.com-filter *
$DeviceInfo = @()
Foreach ($Device in $Devices) {
$DeviceInfo += [PSCustomObject]@{
"Name" = $Device.Name
"ObjectGUID" = $device.ObjectGUID
}
}
Not really sure what I can do with this code.

Appreciate your help

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,514 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,561 Reputation points
    2022-09-14T18:19:24.943+00:00

    There's no need to store the PSCustomObject in an array.

    Get-ADComputer -Server domain.com-filter * |  
        ForEach-Object {  
            [PSCustomObject]@{  
                "Name"       = $_.Name  
                "ObjectGUID" = ($_.ObjectGUID -replace "-", "")  
            }  
        } |Export-Csv "Your-CSV-File-Name-Here" -NoTypeInformation  
    

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 46,561 Reputation points
    2022-09-14T14:49:25.527+00:00

    Try either of these:

    $x = "7a42fc27-02e1-4a82-a2fd-b27ebbd382d2"  
      
    # this . . .  
    $x -split "-" -join ""  
      
    # or this . . .  
    $x -replace "-", ""  
    

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.