1

I've been trying to figure out a way to use Powershell to select only one file (doesn't matter which one) out of a folder so I can do a rename on it. After that I want it to just exit. Please be patient with me, I'm new to PS, most of my coding experience is in other languages.

So I've been doing something like this so it just executes once and exits...

$a = 0 
DO
{
 $a
 $a++
} While ($a -le 0)

Would I put a Get-ChildItem in there somehow, or how do I reference the file so I can rename it to something like "newfile.txt"? Plus the file location is not local on my machine, it's on a UNC path so not sure how to point it to another location.

Thanks in advance.

4
  • 2
    Get-ChildItem filesystem::\\UNC\path\ -File | Select-Object -First 1 | Rename-Item -NewName newfile.txt Commented Feb 28, 2018 at 3:06
  • Use Get-Help Get-ChildItem -full and similar for all the new cmdlets you want to use. Most come with examples... Commented Feb 28, 2018 at 3:31
  • Okay, I have to ask: why are you randomly renaming a single file? Commented Feb 28, 2018 at 8:56
  • I am trying to single thread the processing of a series a files. They are picked up by another platform once they are named to a certain convention. Thank you all for your help! The examples worked! Commented Feb 28, 2018 at 14:34

1 Answer 1

3

Try this:

Get-ChildItem "\\Server\path" -file | select -first 1 | Rename-Item -NewName "newname.txt"
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is actually the version I went with. Appreciate the help, this project has been quite a learning experience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.