1

I have a script that scans for a specific folder in users AppData folder. If it finds the folder, it then returns the path to a txt file. So we can see the computer name and username where it was found.

I would like to be able to format the what is actually written to the text file, so it removes everything from the path except the Computer and User names.

Script:

foreach($computer in $computers){
    $BetterNet = "\\$computer\c$\users\*\AppData\Local\Google\Chrome\User Data\Default\Extensions\gjknjjomckknofjidppipffbpoekiipm"
    Get-ChildItem $BetterNet | ForEach-Object {
        $count++
        $betternetCount++
        write-host BetterNet found on: $computer
        Add-Content "\\SERVERNAME\PowershellScans\$date\$time\BetterNet.txt" $_`n
        write-host 
    }
}

The text files contain information like this

\\computer-11-1004S10\c$\users\turtle\AppData\Local\Google\Chrome\User Data\Default\Extensions\gjknjjomckknofjidppipffbpoekiipm

\\computer-1004-24S\c$\users\camel\AppData\Local\Google\Chrome\User Data\Default\Extensions\gjknjjomckknofjidppipffbpoekiipm

\\computer-1004-23S\c$\users\rabbit\AppData\Local\Google\Chrome\User Data\Default\Extensions\gjknjjomckknofjidppipffbpoekiipm
1
  • Please show a specific example - by directly updating your question - of the information you want to write to the target file: is it, e.g., \\computer-11-1004S10\c$\users\turtle, or only, e.g., computer-11-1004S10 turtle? Commented Jan 27, 2017 at 2:53

2 Answers 2

2

If you have each line in a form of the string $string_containing_path then it is easy to split using split method and then add index(1) and (4) that you need:

$afterSplit = $string_containing_path.Split('\')

$stringThatYouNeed = $afterSplit[1] + " " + $afterSplit[4] 

You can also use simple script that will fix your current logs:

$path_in = "C:\temp\list.txt"
$path_out= "C:\temp\output.txt"

$reader = [System.IO.File]::OpenText($path_in)


 try {

    while($true){

        $line = $reader.ReadLine()

        if ($line -eq $null) { break }

        $line_after_split_method = $line.Split('\')

        $stringToOutput = $line_after_split_method[1] + " " +    $line_after_split_method[4] + "`r`n"

        add-content $path_out $stringToOutput 
    }

    add-content $path_out "End"

}

finally {

$reader.Close()

}
Sign up to request clarification or add additional context in comments.

2 Comments

@Joe Clark is see someone changed the content of text file so each line starts \\ instead of \ so in this case please use indexes [2] and [5] instead of [1] and [4]
Thank you. Had to figure out how to cast to a string. but got it working with this.
2

If you split your loop into two foreach loops, one for computer and user directory it would be easier to output the name of the user directory.

$output = foreach($computer in $computers){

    $UserDirectories = Get-ChildItem "\\$computer\c$\users\" -Directory
    foreach ($Directory in $UserDirectories) {
        $BetterNet = Get-ChildItem (Join-Path $Directory.fullname "\AppData\Local\Google\Chrome\User Data\Default\Extensions\gjknjjomckknofjidppipffbpoekiipm")
        Add-Content "\\SERVERNAME\PowershellScans\$date\$time\BetterNet.txt" "$computer $($Directory.name)`r`n" 
        write-host BetterNet found on: $computer
        $BetterNet
    }
} 
$output.count

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.