Hi,
I’m fairly new to Powershell. What I have is a text file that lists users, locations, and what they have access to at that location. Here is an example of I’m working with:
IEA0A,AA,.21131711…115111917.
IEH6F,AC10,…1…1…1…1…1…
Each number (or period) represents an access right to a part of the program. What I need is to separate each access into an object and remove the columns that I do not need (Lets say that all I need is the 1st, 5th, and 12th.
Here is what I have so far. Not much, but it strips away all the extra stuff in the report and leaves just the data.
$raw = Get-Content C:\file.txt | Where-Object {$_ -like ’ I*'} | ForEach-Object {$_.trim()}
$raw -replace “\s+”,“,”
Thanks,
i think this is good starting point to work with objects
PS C:\Users\alex\Desktop> $txt = Import-Csv .\test.txt -Delimiter "," -Header User,Location,Access
PS C:\Users\alex\Desktop> $txt
User Location Access
---- -------- ------
IEA0A AA .21131711.......115111917.
IEH6F AC10 ....1..1.........1..1..1..
PS C:\Users\alex\Desktop> $txt[0]
User Location Access
---- -------- ------
IEA0A AA .21131711.......115111917.
So, how would I go about getting the following result?
User Location Access1 Access2 Access3
IEA0A AA . 2 1 <–and so on
by the way , each line is an array of properties for the csv object , and each property is an array of characters
that means you can access each line’s “User” with it’s key , indexed by zero and/or each access character indexed by zero , like so
$txt.User[0] or $txt.access[0][0] , which means you can loop through each line/access string
You could do something like this:
$data = Import-CSV C:\Users\Rob\Desktop\Archive\test.csv -Header User,Location,AccessFull
$data |
Select User,
Location,
@{Name="Access1";Expression={$_.AccessFull[0]}},
@{Name="Access2";Expression={$_.AccessFull[1]}},
@{Name="Access3";Expression={$_.AccessFull[2]}},
@{Name="Access4";Expression={$_.AccessFull[3]}},
@{Name="Access5";Expression={$_.AccessFull[4]}},
@{Name="Access6";Expression={$_.AccessFull[5]}}
This is exactly what I needed. Thank you Rob.