Link to home
Start Free TrialLog in
Avatar of CES
CES

asked on

Powershell - Save filename to variable

Does anyone know how to save/pipe the name of a file to a variable  using Powershell?

For example, say I have a file c:\temp\test.txt

Is there a way to get "text" (<ideally) or "text.txt" to be stored into a variable that I can use later

I have not had any luck with Get-ChildItem or Get-Content.  Thanks!
SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CES
CES

ASKER

([System.IO.FileInfo]"c:\temp\test.txt").BaseName    # test

this is doing exactly what I need.  Is there a way to adapt it to and unknown file name, (i.e. the file name could be test.txt, testtwo.txt etc...) and still grab the BaseName?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CES

ASKER

This should do the trick.

I'm basically trying to create a dynamic report...

The files are being produced by a separate process, so depending on options, the name of the file that I want to get into a variable (to be used later in my reporting) can be different.
Avatar of CES

ASKER

Thanks for the help.  These commands got me to where I needed to be