How to copy files from a source with a changing name

Hey Everyone.

I’m just asking how to do this basic function.

How can I do a Copy-item when the source name varies.

What I mean by that is, I want to take a script and put it on my flash drive. Then insert the drive into another computer, and run the script.

The problem I have is the drive letter changes for flash drives, so how do I account for this?

I basically want to do something like this:

Copy-Item "Folder_The_Script_Is_Stored_In_On_The_Flashdrive\randomfile.txt C:\Windows


You could write up a script and put it on the USB drive. Something like this:

Copy-Item -Path .\somefile.txt -Destination C:\Windows

.\ designates the present working directory of the script, so it won’t rely on a drive letter.

when using a Flash Drive the Drive letter can chagne try this .You can even dot source it

$x = get-WmiObject -Class win32_logicaldisk | select -Property deviceid, volumename | findstr YourUSBName
$x = $x.Substring(0,2)
. "$x\Path\To\Your\Script.ps1"

Thank you guys very much! using the ./ worked great.

I would like to mention for anyone else who has this problem in the future. Testing this function in the developer window of powershell will not work! I’m not sure how the powershell ISE runs scripts, but the ./ directory is different, my assumption is it creates a temp file to run the script.

Example: if you saved the script ps1 file to a folder on the desktop and did a copy-item ./test.txt it will give you an error because it wont be able to find it, since powershell ISE runs scripts somewhere else.
Now if you ran the actual ps1 file in the folder on the desktop it will work.

(Again I’m new to this, as in my first week of scripting. So please bare with me if my terms are off)

Thanks again,