We help IT Professionals succeed at work.

Powershell - Save filename to variable

CES
CES asked
on
2,598 Views
Last Modified: 2017-04-05
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!
Comment
Watch Question

Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CESNetwork Administrator
CERTIFIED EXPERT

Author

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?
PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CESNetwork Administrator
CERTIFIED EXPERT

Author

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 Administrator
CERTIFIED EXPERT

Author

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions