I am would like to read in the text after the last backslash from my text file. Currently I have:
$data=Get-Content "C:\temp\users.txt"
The users.txt file contains path from users home directories
\\myserver.home.com\users\user1.test
How can I pick out the users account (user1.test) name at the end of the line of text so I can use it as a variable?
You can use a simple regex to remove everything until and including the last slash:
$user = $data -replace '.*\\'
Since you are dealing with file paths, you can use GetFileName:
$data=Get-Content "C:\temp\users.txt"
$name=[System.IO.Path]::GetFileName($data)
$HomeDirArray = Get-Content "C:\temp\users.txt" | Split-Path -Leaf
will give you an array that can be iterated through using ForEach
(e.g., ForEach ($User in $HomeDirArray) {...}
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With