How to convert powershell onliner to PS1 - powershell

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!

Related

Copy Command Prompt's content to clipboard

I'm looking for a solution to copy the command prompt's current content to the clipboard. I know something similar is possible by redirecting the output or using clip but I'm looking for something else. I'm looking for a command which copies the entire content of the command prompt window to the clipboard/a file anytime when it's called(not just the output of one command). It's quite easy to do manually by selecting everything and press ctrl-c but I need a command for this.
Basically it should achieve the same as doskey /history > somefile.txt but saving the output of the commands too.
Is something like this possible?
you can use simple java program , to get the entire content of the command prompt and out it into a String variable or write it to txt file

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 achieve a two line Prompt?

In the batch language of Microsoft's CMD.EXE console window, I never liked having my command start at the far right, after a long display of the directory path. So in my Control Panel → System → Advanced System Settings → Environment Variables I saved the following assignment, where $_ is like a Soft Return:
PROMPT=[$P\]$_$+$G$S
The displayed prompt was two lines like this:
[C:\Temp\]
>
(The $+ tracks pushd and popd, the fancier than chdir commands. $S is space. By the way, the ^ character a line wrap/continuation character in batch, just as backtick ` is in PowerShell.)
Now I want the same-ish two line prompt in PowerShell. There is good news and bad news.
The good news is I can achieve that in my open PowerShell window by typing at the > prompt:
function prompt {'[' + $(get-location) + '\] SHIFTENTER > '
(By SHIFTENTER I mean press Shift+Enter, what I think might be called a "soft return"?)
....... BAD NEWS, PROBLEM ......
I want to put the above function prompt ... line into my profile PowerShell script, namely Microsoft.PowerShell_profile.ps1 (at path $Profile). But how?
Notepad.exe has no support for Shift+Enter.
MS Word understands Shift+Enter, but when I SaveAs .txt, and then examine with Notepad++, I see a plain CR-LF (meaning \r\n, 0x0d 0x0a).
Notepad++ menu Edit → Character Panel enables me to insert special ASCII characters into my .txt / .ps1 file, such as 0x0b called VT (for "vertical tab"). But despite some claims on websites, VT is not behaving like a Soft Return when I use it in my function prompt ... profile .ps1 file (I also run the profile .ps1 script to retest).
Can the prompt I want be established by a profile .ps1 script?
The PowerShell equivalent of your batch-prompt is:
function prompt { "[$(Get-Location)\]`r`n$("+"*(Get-Location -Stack).Count)>" }
#`r`n is just a shorter way of writing [System.Environment]::NewLine
Add it to the profile to suits your needs:
AllUsersAllHosts:
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersPowerShell:
C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
AllUsersISE:
C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1
CurrentUserAllHosts:
C:\Users\username\Documents\WindowsPowerShell\profile.ps1
CurrentUserPowerShell:
C:\Users\username\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
CurrentUserISE:
C:\Users\username\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

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 to make the input not automatically execute when I paste in PowerShell

I have a feeling that this is a very simple fix, but whenever I try to paste into PowerShell using right-click, it automatically executes whatever it is I am trying to paste. The function is finding a computer name based on an input using AD and then copies the computer name to use in other functions. Here is the code for only the part that does the copying:
$strComputer = $output.Name
$strComputer | clip | Out-Null
So when I right-click to paste, it executes whatever $strComputer equals and obviously throws an error. Thanks!
The problem with pasting something into PowerShell (and clip.exe is very good example) is the fact, that any newline (and clip.exe used like that will add newline at the end) will trigger same reaction that you pressing enter would.
There are at least two ways around it:
paste without newlines (e.g. use [System.Windows.Clipboard]::SetText($output.Name))
if you are on v3 - use PSReadLine module and CTRL+V
For the first one - be sure to run Add-Type -AssemblyName PresentationCore first - this assembly is loaded by default only in PowerShell ISE. If you are on v3+ though I would really recommend to give PSReadLine a try.
I've found an easy solution, that doesn't require any Powershell modifications.
Steps:
Type if ($true) { into the terminal.
Paste in the code-block. Because you started an if-block, but didn't provide an end-bracket for it, Powershell interprets the code-block as incomplete, and so awaits the end-bracket before running anything.
Now you can use the arrow keys to edit the code block however you want.
When you're done with your edits, add the missing } to the very end, then press enter to execute it.
Alternatively you can use the ISE, where you can paste into the Script Pane (the top half), and edit until it appears as desired, and run when you want to.