Powershell GUI for various scripts to be executed - powershell

I have a list of Powershell scripts that perform various functions
im trying to see how i can create a GUI using those scripts .
Say like i have :
Button 1 : Disk Cleanup
Button 2 : Copy Files ( Should have a source and destination path being taken from Edit Box : meaning paths can be provided in the Text box and my scripts to take the values from that path to execute)
Button 3 : Clear History
I'm trying to use the New-Object System.Windows.Forms.Button but unable to get the button to execute the script and even if it does execute first script its failing to execute second one .
Has anyone implemented this type of feature earlier ?

Related

How do I prompt for a path to a directory with support for tab completion in the console?

I want to prompt the user for a directory path.
The user can enter some text like C:\Pro in command line, tap tab button and the script should autocomplete the text toC:\Programs\ on another tab it should change the text to C:\Program Files\.
So, the script should scan folders and help user to navigate between them on typing.
Important: I can't use any GUI dialogs, because I am going to use the script on Win with no GUI.
This code will not work:
New-Object System.Windows.Forms.FolderBrowserDialog
Ummm... you don't have to? PS already does that by default?
function T
{
param
(
[System.IO.FileInfo]
$P
)
$P.FullName
}
T -P # Pressing <tab> at this stage will automatically show you the first file in your working directory. That happens even if the P was of type string.

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

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

How do I write handles from a MATLAB GUI into a .m file?

I am trying to create a GUI for a script that receives paths and matrix names from the user (via uigetdir and uigetfile functions as well as edit text fields), and finally upon pushing the "Run" button writes these handles into a .m file and runs another script.
Essentially, in the end it should create a file called 'RunName'.m that looks something like:
base_path = get(handles.BasePathEdit,'String')
file_name = get(handles.FileNameEdit,'String')
main % runs the main script with the preceding variables defined as they were
and run it.
I'm guessing the script would run successfully if I just write it like that under the 'Run' push-button function, but I would still like for it to create a .m file as described.
Thank you very much for you help,
All the best.
The simplest way of accomplishing this is to write a function that executes upon the pressing of the 'run' button that reads the values from the GUI and the proceeds with the main script. In guide, this is simple - right-click on the 'run' button, and under 'View Callbacks', select 'Callback'. Then you can use the usual syntax get(handles.RELEVANT_TEXT_BOX,'string') etc to read the resr of the data in. You can then pass this to your main function, or you could then just copy and pase your script straight into the callback.

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>

Running "preamble" code in MATLAB

Is there any way to make MATLAB run a certain chunk of code every time you try to run a script? For instance, I would like MATLAB to run
sprintf('Here we go...')
as soon as I hit the Run button and then move on to execute my script, so if my script were
i = 1;
i = i * i;
display(i)
I would get
ans =
Here we go...
i =
1
P.S. I would appreciate it if the people with higher reputation please corrected the title of my question for it to better reflect the content.
as soon as I hit the Run button
I am assuming you are talking about the run button in the editor. In R2012a there was a feature called "Run Configuration". A run configuration was linked to a specific script and included code to be executed prior to the script being run. There does not appear to be a global setting to be used on all function. This feature appears to have been silently removed in R2012b.
In R2013b you can chose to run a different script. Presumably you could hack the editor to get the current file and use the custom run script to run your preamble and then the current editor file. This seems like a lot of work for not much return ...
You could create a file called myrun.m
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor;
title = jEditor.getTitle;
currentFilename = char(title.replaceFirst('Editor - ',''));
fprintf('Here we go...');
run(currentFilename);
and this in the editor under run Run: type code to run type myrun. One you do this once it will remember your preferences and you can then run you code via myrun with F5. It will remember your preferences across restarts.
The way to do this would be to have a preamble.m and doThis.m. In preamble.m you'd have this:
sprintf('Here we go...')
and then in doThis.m, you'd have
preamble
i = 1;
i = i * i;
display(i)
The only trick to making this work is to have them both on the path, or in the same directory.
Not sure if I got what you want, but you can divide Your m file into Code Sections. For example:
%% Section 1
sprintf('Here we go...')
%% Section 2
i = 1;
i = i * i;
display(i)
The %% is a section break. Place your cursor in the relevant section, and on the Editor tab, in the Run section, click Run Section. (or press Ctrl+Enter)
see here for more info.
If you only want this for one (or a few scripts) either add the command in the script, or make a wrapper function/shortcut.
If you want this for many scripts without input, you can create a generic wrapper:
Suppose you want to run things like myFun(a,b,c) then create a wrapper that you can call like this:
myWrapper('myFun(a,b,c)')
Then you can first call your display command and then use eval on the input of myWrapper. Note that this becomes cumbersome if your function call is multiline or contains quotes.
If these solutions can't help, you probably need to ask yourself why you are trying to do this and whether there is a better solution for the underlying problem.