Change Directory to Folder Containing PowerShell Script - Regardless of Where That Folder Is Located - powershell

I have a script that I've created to prep our customer's servers for a software install. Part of this requires the script to be run as administrator, so just instructing people to click "Run With Powershell" doesn't get the job done. The script is in a folder with a number of .ini files that the script needs to copy to different server locations. If I just right-click the Powershell script and select "Run With Powershell," it is able to find the files and copy them without issue. Unfortunately, if I open the script in ISE, it opens with a default directory of C:\users\user, and I can't seem to copy those .ini files without first running a change directory command to get us to the folder that the script and the .ini files are in. But I'd like our installation techs to be able to run this without worrying about the exact location they initially drop these folders. I'd also like them to not have to worry about changing the directory manually in PowerShell. Some of our customers have multiple drives, and it might make sense to put this stuff on something other than the C drive, so it's hard to tell where this folder might end up. But I'm not sure of a command that will get me to the directory of the *.ps1 file, without knowing where that file is beforehand... Anyone have a suggestion?

You can use $PSScriptRoot that will have the location of the directory where the script is located.
This is referenced in the following post:
How can I get the file system location of a PowerShell script?

Related

File Explorer won't display PowerShell Modules directory otherwise visible in $env:psmodulepath

I'm in the process of learning how to create powershell modules through this youtube tutorial https://www.youtube.com/watch?v=yKstEJKdc4o&t=3661s - the video itself is not important.
I learned from learn.microsoft.com that powershell will have the following module directories by default:
$PSHome\Modules (%Windir%\System32\WindowsPowerShell\v1.0\Modules)
$Home\Documents\WindowsPowerShell\Modules (%UserProfile%\Documents\WindowsPowerShell\Modules)
$Env:ProgramFiles\WindowsPowerShell\Modules (%ProgramFiles%\WindowsPowerShell\Modules)
however there is no C:\users\john\Documents\WindowsPowerShell\Modules folder visible in File Explorer, while it's clearly visible in $env:psmodulepath when I run this from PowerShell. I'm able to navigate all the way to C:\Users\john\Documents\WindowsPowerShell but there is no Modules directory. I did enable hidden folders display and did also check Documents folders for OneDrive, Public, Administrator users. This is not a work/school computer, this is my own PC.
Can someone please help me understand why this Modules folder is visible in $env:psmodulepath but not in File Explorer?
Just because a location exists in $env:psmodulepath, doesn't mean it actually has to exists in the file system.
You can add any random location to $env:psmodulepath without it actually existing. PowerShell will just skip any locations that don't exist.
With a fresh Windows account, there isn't even a WindowsPowerShell directory under $env:userprofile\documents, to use a user profile or modules you have to create those directories.
It is fairly easy for a user to create a new directory, but a little bit more tricky to add a new entry to $env:psmodulepath. I guess that is why they already put it in there, even though it may never be used.

Powershell accessing executable in wrong directory despite $PATH variable being set

I was trying to get vim going in powershell, and so I added C:\Program Files (x86)\Vim\vim82 to the PATH. However, when I try to run vim in powershell, it goes to the wrong folder, C:\Program Files (x86)\vim\vim80\vim.exe (note that it is incorrectly vim80 instead of the correct folder name of vim82).
There's nothing else in the path that would send it to vim80, and the vim80 folder doesn't even exist on my computer, so I'm kind of confused as to how that might happen...
And the desired vim.exe executable does run successfully if I actually navigate to the correct vim82 folder.

File selected in WindowsExplorer with Preview Pane locks the file so powershell cannot output to that file

I have a scheduled script that outputs bunch of HTML files with static names to a remote location. I noticed, that if I have one of those files selected in Windows Explorer so that its contents are shown in Preview Pane, then Powershell cannot overwrite that file and skips updating it.
This only happens if output files are in remote location. Works just fine if files are local.
How do I force PowerShell to overwrite remote files in this situation? Lots of users work with those reports and if one of them leaves Windows Explorer window with one of those files highlighted overnight when the script runs, the file is not going to be updated.
Move HTML files to webserver. You will solve your problem entirely. IIS Setup on windows server is Next, Next, Next. You can leave link to a new file location (https://....) in old place, so users can easily navigate to a new place. Possibly this link can be automated (not sure because of modern security standards)
Try [System.IO.File]::Delete($path) just before writing this file. This removes file entry from filesystem, but leaves file open for those who have it open for now. This makes your script to write to a new file with the same name. Old file exists without name (deleted) but leaves open until everyone close it. Check it actually deleted with resresh!
Try [System.IO.File]::Move($path, $someTrashFullName) just before writing this file. $someTrashFullName probably must be on same drive. Same as Delete, but renames file. Some self-updating software use this strategy. File is renamed, but it's still kept open under new name.
Try replace file with shortcut to some file. You can generate files with different names and change shortcut programmatically
HTML files that change location using js ? They read nearby JSON (generated by export script) and lookup there for a new filename. So user opens static unchanged A.html, JS inside lookups at A.json for new name and redirects user to A-2020-08-11.html. I'm not sure browsers allow reading JSON files from JS for files that opened from network drive.
Only way left is to stop network share or\and close open files server-side.
Maybe some fun with to disable preview in this folder \ completely?
Try with -Force. But to me, it seems to be more a permission issue.
Remove-Item -Path '\\server\share\file' -Force

If files exist in directory,move it powershell

I am a PowerShell beginner
May I have a question,
Is there any PS script that can always detect files in a directory in the background,
If files exist in the directory, move it to another location.
I tried using .PS1 (Move-item cmdlet) on taskscheduler, but it can't be work
Can Powershell do that?
Google for FileSystemWatcher and PowerShell, FileSystemWatcher allows you to specify a directory to watch for changes and then act on those changes. Like if a new file is detected, you can move it.
It seems there are several examples of scripts doing this out there.

Find path from where .exe is run Autohotkey

I have a compiled ahk script in which I use %_A_WorkingDir% to get the current working directory. But now I'm calling the .exe from another file two folders up so %_A_WorkingDir% returns the directory that is two folders up, not the location of the actual .exe. How do I fix this issue?
A_WorkingDir is the directory in which the script is currently
working. You can change it by using
the command SetWorkingDir.
A_ScriptDir is the full path of the directory where the script is in.
A_ScriptFullPath is the full path of the current script.
https://autohotkey.com/docs/Variables.htm#prop