Passing the current File Path through to Powershell "SendTo" Script - powershell

I'm trying to create a script that will let me copy an item from one location to a specified location in a PowerShell script. Before it's mentioned, I know that I can put a shortcut in the Send To directory to perform this action, but I would like to add additional features (that's for a different time). I do apologize if there is another post relating to this, I've been looking for a day and a half to find one.
What I would like to know is if there is a way I can pass the current-item-that-I-am-right-clicking's file path to PowerShell to be used with the Copy-Item function. So if there is an item on the desktop, I can use this PowerShell script to Copy-Item C:\Users\USERNAME\Desktop\File.ext using the path as a variable (I believe that would be the appropriate term) from the "Send To" Selection in the context menu.

You don't have to do anything special to get the Send To context menu to send the file path to the target - it happens automatically.
Try the following:
Create a new script, let's call it C:\Copy-SendTo.ps1
param($SourceFile)
Copy-Item $SourceFile -Destination C:\your\specific\location\
Now, create a shortcut in $env:APPDATA\Microsoft\Windows\SendTo with the name "CopyTo" and the following target:
%windir%\System32\WindowsPowerShell\v1.0\powershell.exe C:\Copy-SendTo.ps1 -SourceFile
Now right-click a file on your desktop, select Send To > CopyTo.
Voila, your file has been copied

Related

Setting path for AHK's FileSelectFile function not working

My goal is to create a script that will allow me to paste images into a social media platform's new post instance. This platform allows pasting images from Clipboard.
My logic is:
Open the File Select modal with a specific default path.
Select a file by user.
Pass the selected object to the Clipboard.
Send the Paste command back into the Post instance.
I was able to complete all of this; the only issue I'm having is setting the absolute starting path of the File Select modal; no matter what I do, or how I pass the absolute path, it seems to always open the path of the last physical action outside the script, in other words, if I physically opened a Explorer from Windows itself or any other app, it will use that path, instead of the one I mentioned in the script; it's been driving me nuts. I've rebooted the script, refreshed everything, cleared cache, did a rain dance.
Would love some assistance!
Here's my script:
^+x::
mypath := "C:\Users\MyComputerName\Downloads\SOCIAL-MEDIA\Images"
FileSelectFile, SelectedFile, 3, %mypath%
if (SelectedFile = "")
MsgBox, The user didn't select anything.
else
pToken := Gdip_Startup()
Gdip_SetBitmapToClipboard(pBitmap := Gdip_CreateBitmapFromFile(SelectedFile))
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)
send ^v
return
It seems that setting the third parameter to "2" aka "Path must exist," did the trick.
https://www.autohotkey.com/docs/v1/lib/FileSelectFile.htm

Task Scheduler | I am trying to schedule a .ps1 file but every time the scheduler runs a pop window appear "How Do you want to open this file?"

Here's a screenshot from scheduled task action settings
First of all make sure the path used for powershell is correct (best option in my opinion is to click Browse... and select the program with the dialog window instead of manually inserting the full path, if that's what you did).
In second place, change Add arguments (optional): to -File "D:\TM1 Model\Test GIIS\...\yourfilename.ps1", please note you should use double quote since the full path contains spaces; again, make sure the full file path is typed correctly.
There should be no need to insert the full path in the in the Program/script box. Most people don't even know where it is.
For all the PowerShell scripts that I run on servers I only type powershll.exe and it works.
I agree with Giacomo (Calvin?), the value in the Add Arguements box needs to be in quotes if you have spaces in the folder or file name. ie -File "C:\Batch Files\checkdisk.bat"
The -File argument is telling PowerShell the file you want powershell.exe to run.

How to convert powershell onliner to PS1

I have a powershell one liner that would like to transform on a PS1 file
This question is pretty basic but how do I convert apowershell one liner to PS1?
Thanks
There are a few steps to do this:
Open notepad.
Copy and paste the powershell script into notepad. Now if the one-liner has multiple lines using ; separate them by using enter. You can expand The for, if, while, try blocks like this:
try
{
Anything
}
You can also make the script accept args using $args automatic variable. $args[0], $args[1] .. $args[n] parameters and so on.
Select file menu and save as dialog.
Enter the name of powershell script with extension .ps1 (Example: foo.ps1).
Choose "All files" in the drop down menu.
Click save button. Here you go!

Make PowerShell suggests current folder content/items using the Tab key

I'm new to PowerShell,
I'd like to navigate through the file system by getting the current folder content suggestions instead of cmdlets by clicking the Tab key just like in the CMD.
I don't want to use the dir cmdlet, I want to walk through the different items by clicking on the Tab key.
Is that possible and how to do it ?
If you type ex. a<tab>, you will always get file/folders starting with a before it shows any cmdlets and commands in PATH-locations.
If you only want files and folders, type .\<tab>

Is it possible to change the default value of $profile to a new value?

So I would rather not create my profile file here:
C:\Users\fmerrow\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
I mean don't get me wrong, this isn't the end of the world and I can live with it. However, I like to keep root "My Documents" reasonably lean and I really would rather not create a directory there every time I start using a new application.
I've nosed around looking to where this setting might be hidden, but so far no luck. It doesn't seem to be in the registry or any of the $PsHome files.
Do I just have to learn to live with this? . . . or is there a way to change the value of $profile that will "stick" on this system for all time? That is, to change the "default value" of $profile?
The best I've thought of so far, is to ignore $profile and instead put some code in $profile.AllUsersAllHosts to source/execute my file from where I want to put it instead of from the default $profile location.
Comments and/or other suggestions welcomed.
Frank
The only thing I can think of is "dot sourcing" your profile at the powershell invocation.
For example:
powershell -noprofile -noexit -command "invoke-expression '. ''C:\My profile location\profile.ps1''' "
By changing the script that invoke-expression command points to you can place your "profile" anywhere you'd like. Then, create shortcut that launches PowerShell and set the target to the above command.
You can change your $Profile.CurrentUser* paths by changing your personal folder path Environment.GetFolderPath(Environment.SpecialFolder.Personal)
Either via regedit
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders
Under the Name column select Personal and chage the value to where you want your profile.
Or via PowerShell
New-ItemProperty
'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders'
Personal -Value 'Your New Path Here' -Type ExpandString -Force
You have to reboot for this to take effect.
You can also put your profile file here
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
If you want to have a separated location for all your profiles and scripts, you can modify your profile.ps1 file above as
$profile = "NewLocation\profile.ps1"
. $profile
Make sure what type of profile you use, see details here
https://technet.microsoft.com/en-ca/library/dd819434.aspx
Try junctions by running this command in powershell:
cmd /c mklink /J c:\Users\Name\Documents\WindowsPowerShell\ d:\Powershell\Engine\Profile\
For more information about junctions see here.
I think your solution to source your "new" profile in the existing profile is probably as good as you're going to get.
This solution is inspired by RootLoop's answer:
Access your profile by navigating to its location defined by $PROFILE. (For me, that location happened to be C:\Users\<username>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1. Then, go ahead and move the contents of your customized profile to wherever you want it to be, (C:/NewLocation/profile.ps1, let's suppose). Replace the original profile's contents (the file C:\Users\<username>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1) with the text:
$profile = "C:\NewLocation\profile.ps1"
. $profile
Remember that the profile is just a script that is run as soon as you open powershell. This script above will first set $profile to the new location, so any references to the $profile variable will still work as if you moved it. The next line of code will invoke the new profile with syntax that is called dot sourcing. Effectively, the . $profile line is just running your new profile code.
Before that will work on your system, you may have to loosen your execution policy. See https://superuser.com/questions/106360/how-to-enable-execution-of-powershell-scripts for details on that.
Next, you can reduce the clutter in your My Documents directory by hiding the Powershell folder. Simply right click on the folder, select "properties", and under the general tab, select "hidden". And voila! - You have effectively created the illusion that you moved your profile location, without having to do much tinkering with system settings!
According to Scripting Guy article Understanding the Six PowerShell Profiles, $profile is expanded from $PsHome\Microsoft.PowerShell_profile.ps1; $pshome is the powershell installation directory and a read-only variable; according to a post on this thread, Microsoft tells us this cannot be changed.
This might be more of a workaround, but what I did was create a symbolic link copy of the WindowsPowerShell directory in the location PowerShell was looking at. This is more of a bandaid technique though.