I need some help splitting a string into 2 separate columns. I am using get-content to retrieve the data. However, there are no headers in the files I am using. This is what the files look like:
USER1 11/21/2013 13:59
USER2 11/21/2013 14:07
USER3 11/21/2013 14:10
USER3 11/21/2013 14:13
It is gathering the last logon times for users on a particular machine. I want to use get-content to retrieve the data and then turn it into an object with 2 columns, “User” and “Logon Date.”
I can retrieve the content without a problem, but it isn’t formatted nicely. Ultimately, I will be piping the object to out-gridview:
Get-Content \\SERVER\gpotest$\USERS\$Computer.txt | select -Last 10 | Out-GridView -OutputMode Multiple -Title "Logon for Last 5 Users"
Any ideas how I can split each line into 2 columns based on either the space between the user and the date or splitting where an integer starts (the date)? Any help will be greatly appreciated.
Personally, I’d use Import-Csv for this. Your file looks like a pretty well-behaved, space-delimited file (without a header row), and you can use Import-Csv to handle that like this:
Getting closer. However the code you put above only creates 2 columns. First “User” column includes the user and the date and the “LogonDate” column includes the time.
Ah. I can’t see it from your post here, but I suspect what you actually have is a tab-delimited file, which is even easier. Can just use Import-Csv without Select-Object, if that’s the case: