Quentin M.
Quentin M.

Reputation: 61

How to recover only the file name without the path and file extension?

I would like to create a script powershell, but for it to work properly I need the MAC address of the workstation I am looking for (they are thin clients). For that I want to get the name of the configuration file in this form: $mac.ini.

For that I tried an interesting command that works well: I enter the name of the Wyse (thin client) in the command and I send, but unfortunately I get a result in this form:

Name
----
\\***\WnosIniManager\inc\847BEBEF****.ini

or like that with a "[string]":

@{Name=\\***\WnosIniManager\inc\847BEBEF****.ini}
$fromDir = "\\***\WnosIniManager\inc\"
$mac = Get-ChildItem -Path $fromDir\*.ini |
       Select-String -Pattern "TWYD512" |
       group Path |
       select -Unique Name

I only need the file name, and no extension, I have to remove the path and extension to keep only the MAC address like this: 847BEBEFABAB.

Upvotes: 2

Views: 311

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200523

Run Select-String in a Where-Object filter, then expand the BaseName of the files.

Get-ChildItem -Path $fromDir -Filter '*.ini' | Where-Object {
    Select-String -Path $_.FullName -Pattern 'TWYD512' -SimpleMatch
} | Select-Object -Expand BaseName -Unique

Upvotes: 2

Related Questions