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!
Powershell

Avatar of undefined
Last Comment
CES

8/22/2022 - Mon
SOLUTION
Chris Dent

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
Chris Dent

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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.
CES

ASKER
Thanks for the help.  These commands got me to where I needed to be
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck