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!
CESNetwork AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris DentPowerShell DeveloperCommented:
There are a number of ways depending on what you're looking to get. The first is to use the Split-Path command.
Split-Path c:\temp\test.txt -Leaf    # test.txt

Open in new window

If that isn't sufficient, we can do things with .NET.
([System.IO.FileInfo]"c:\temp\test.txt").Name    # test.txt
([System.IO.FileInfo]"c:\temp\test.txt").BaseName    # test

Open in new window

CESNetwork AdministratorAuthor Commented:
([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?
Chris DentPowerShell DeveloperCommented:
If the file exists, Get-Item / Get-ChildItem:
$name = Get-Item $thePath | Select-Object -ExpandProperty BaseName

Open in new window

$thePath can contain wildcards, but if you get more than one match you'll have more than one value in $name.

If it does not exist, where does it come from?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CESNetwork AdministratorAuthor Commented:
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.
CESNetwork AdministratorAuthor Commented:
Thanks for the help.  These commands got me to where I needed to be
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.