Note that there are some explanatory texts on larger screens.

plurals
  1. POselect a username from path and append to filename using Powershell
    primarykey
    data
    text
    <p>I am trying to get a specific logfile from each user account on our system. The file is in the same path for everyone and has the same name. The current script just adds a number to the end of the file but I have been told that knowing who each file belongs to would help in the next stage of troubleshooting. This is where I am starting to struggle.</p> <p>I have tried using the initial <code>Get-Childitem</code> to return the directory (in this case a UNC) and then output this to a string by piping it to <code>Out-String</code>. I was then hoping to use replace to trim off the bits of text on either side of the username and append that to the filename. This does not seem to be working at all, though.</p> <p>All directories have the same path:</p> <pre><code>\\files\users\[USERNAME]\TEMP\generic.log </code></pre> <p>I am wondering if I am, as normal, overcomplicating the steps here? Essentially I need to trim <code>\\files\user\</code> and <code>\temp\</code> from the directory path, leaving just the username as a variable, so that I can add to the copy command. My attempts to use <code>-replace</code> tell me that I am using an invalid regex.</p> <p>Code thus far:</p> <pre><code>$file = Get-ChildItem -Path \\files\users\talexander\TEMP\ -Recurse -Force -Include generic.log | Select-Object Directory | Out-String $file -replace "\\xfiles\users\", "" </code></pre> <p>This was a first step to try and get the replace working but, alas, it fails. Grateful for any thoughts.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload