powershellpathunc

With Powershell I want to read a UNC-path from within a file


I'm still a powershell rookie, but thanks to you I'm learning ... :-)

I have a simple text file, let's say its name is "text.txt". In this text file there are some words (bla bla) followed by a UNC-path which is looking somewhat like this:

blah blah jaada jaada \\domain.ad.corp\data\dir1\dir1a\dir1ab\filename.txt blah blah jaada jaada

From this simple example text file I want to read the UNC-Filename into a variable, let's say the variable's name is $unc_content.

When powershell is asked to give me the content of the variable $unc_content, the answer should look like this:

$unc_content

\\domain.ad.corp\data\dir1\dir1a\dir1ab\filename.txt

How could that be accomplished? Many many thx for your helpful tipps.


Solution

  • This code first reads the content of "zxc.txt" using Get-Content. Then it defines a regular expression that matches UNC paths, which seems to be what you're looking for. Finally, it uses Select-String to find all occurrences of the regular expression, and gets the value of the resulting matches using .Matches.Value.

    If there is only one UNC path in the file, $unc_content will contain that path as a string. If there are multiple UNC paths, $unc_content will contain an array of strings.

    Note that if the file doesn't contain any UNC paths matching the provided regular expression, $unc_content will be empty or null.

    $content = Get-Content -Path "zxc.txt" # Read the text file's content
        $unc_regex = '\\\\[\w\d.-]+\\([\w\d\s.-]+\\)*[\w\d\s-]+\.[\w\d]+'
        # This regex will match paths that start with \\ and end with a filename.extension, while allowing for spaces and various characters in the filename and directory names.
        
        $unc_content = ($content | Select-String -Pattern $unc_regex).Matches.Value
        Write-Host $unc_content
    

    Or

    $unc_content = Get-Content "zxc.txt" | Select-String '\\\\[\w\.\-]+\\[\w\-_\\.]+'
    $unc_content = $unc_content.Matches.Value.Trim()
    write-host $unc_content
    

    The Get-Content command is used to read the contents of the "text.txt" text file.

    Select-String is applied to the obtained content to find all substrings that match the given pattern. In this case, we're looking for a string that starts with two backslashes (\), followed by any number of letters, numbers, and additional domain characters, and then the directory and file name (additional characters may also be present, but not special path separator characters).

    $unc_content.Matches.Value.Trim() selects the values found using the pattern, builds a collection of objects from them, and then selects only the values from each object while removing any leading or trailing whitespace.

    The resulting value is saved in the $unc_content variable, and you can use this variable in your code thereafter. Calling $unc_content will return the found UNC path (\domain.ad.corp\data\dir1\dir1a\dir1ab\filename.txt) without any additional spaces or other characters.