L04D1NG
L04D1NG

Reputation: 23

Select-Object output Directory path to Variable

I have a program that I need to uninstall on a number of systems and unfortunately the way this software is silently uninstalled is by running an exe called "uninstall.exe" this could be in 1 of many directories per system so I have a PowerShell script I'm trying to make work where I look for "Uninstall.exe" in any of the directories it may be located with:

get-childitem -recurse -include "uninstall.exe" 'C:\Program Files (x86)\mainoffice-*'

This seems to work as the output gives me the full path of the folder where "Uninstall.exe" is located. I will need to then run:

uninstall.exe --mode unattended

So I think I will need to output the full path where "Uninstall.exe" is located to a variable. I tired to do this with:

$uninstalldir = get-childitem -recurse -include "uninstall.exe" 'C:\Program Files (x86)\wwworksmainoffice-*' | Select-Object FullName

I can't seem to just get this to output the directory path so I can do something like:

$uninstalldir --mode unattended

Can anyone try help me understand where I might be going wrong here?

Upvotes: 0

Views: 604

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

This pipeline:

... |Select-Object FullName

... will create a new object with a single property FullName, so in order to resolve just the string value stored in FullName you'd need to dereference it like this:

$uninstalldir.FullName

This is still just a value expression - it you want PowerShell to invoke a string as if it were a command or program, you'll need the & call operator:

& $uninstalldir.FullName --mode unattended

Alternatively, you could've used Select-Object -ExpandProperty FullName or ForEach-Object FullName to get just the raw value of the FullName property instead of sticthing it onto a new object:

$uninstalldir = Get-ChildItem ... |Select-Object -ExpandProperty FullName
# or 
$uninstalldir = Get-ChildItem ... |ForEach-Object -MemberName FullName

At which point the variable itself resolves to the string value:

& $uninstalldir --mode unattended

Upvotes: 2

Related Questions