Powershell vs GUI shortcuts - powershell

I have a file named new.txt. Using GUI I can create a shortcut, for example, "new.lnk". When I click on "new.lnk" file, Notepad is opened with the contents of "new.txt" file. When I create the shortcut using PowerShell
NEW-ITEM -TYPE SYMBOLICLINK -TARGET "NEW.TXT" "NEW.LNK"
I can see the contents of the file using
CAT "NEW.LNK"
but the shortcut file is not working in the GUI: it does nothing.
I expect to see the contents in Notepad editor. The properties of the file created using GUI and PowerShell are the same, except for "Start in" information: blank when the short cut is created using PowerShell and with the path file directory when using GUI.

Symbolic link (symlink) is not the same as Windows shortcut. A symbolic link is created on file system level - it says "here's a file with such filename, but the content is actually in this other file". It's size is 0 Bytes, as it just points to other file.
It would be more proper to name the file "new-linked.txt" instead of "new.lnk".
A shortcut ".lnk" is a separate file that is interpreted by Windows shell. It contains a path to the target file (among other additional properties). If you create shortcut from UI and then try cat my.lnk, you'll see the content of the shortcut file itself, not the target file.
For creating a Windows shortcut from Powershell, see How to create a shortcut using PowerShell.

Related

Windows Powershell Basic Questions - new user

When trying to open a file with text editor VIM, I am unable to open the file unless VIM (shortcut) is in my current working directory. As an example, I am able to write start firefox to open a firefox window. However, start vim C:\filepath\filename.txt does not work unless a vim shortcut is in my current directory. How do I get around this?
Also, is there a way to have a program execute a file in the current working directory without having to reference the entire file path? For example instead of Start-Process vim C:\Users\User\Desktop\File\file.txt is there an available path shortcut like Start-Process vim ~\file.txt with ~ representing the current working directory?
The OS need to determine the full path of the exe, no matter what.
There's 2 ways that it will happen.
You're calling the executable from it's working directory
The executable location is in the Windows environment variable.
You can view the PATH variable content through this simple statement
$env:Path -split ';' | sort
You sill see that the Firefox path is listed there, but not the one from VIM.
That's why the former can be started by it's executable name and the latter require the full path.
You need to add VIM directory to your PATH variable if you want to be able to call it just by typing vim
Otherwise, if you have restricted access or don't want to edit that variable, you can also set a $vim variable, then invoke it whenever you want to call the executable.
Regarding the second part of your question
Powershell use the dot as a reference to the current directory .\file.txt.
You can also just specify the filename without anything else file.txt.
Both backslash \ & slash / work for filepath so .\file.txt and ./file.txt are both valid ways to reference the file.
Use ..\ to reference the parent directory (e.g. ..\file.txt)
$Vim = "c:\Path\To\Vim.exe"
& $vim "file.txt"
& $vim ".\file.txt"
#Forward slash also work for paths
& $vim "./file.txt"

How to read a text file to a variable in batch and pass it as a parameter to a powershell script

I have a powershell script that generates a report, and I have connected it to an io.filesystemwatcher. I am trying to improve the error handling capability. I already have the report generation function (which only takes in a filepath) within a try-catch loop that basically kills word, excel and powerpoint and tries again if it fails. This seems to work well but I want to embed in that another try-catch loop that will restart the computer and generate the report after reboot if it fails a second consecutive time.
I decided to try and modify the registry after reading this article: https://cmatskas.com/configure-a-runonce-task-on-windows/
my plan would be, within the second try-catch loop I will create a textfile called RecoveredPath.txt with the file path being its only contents, and then add something like:
Set-ItemProperty "HKLMU:\Software\Microsoft\Windows\CurrentVersion\RunOnce" -Name '!RecoverReport' -Value "C:\...EmergencyRecovery.bat"
Before rebooting. Within the batch file I have:
set /p RecoveredDir=<RecoveredPath.txt
powershell.exe -File C:\...Report.ps1 %RecoveredDir%
When I try to run the batch script, it doesn't yield any errors but doesn't seem to do anything. I tried adding in an echo statement and it is storing the value of the text file as a variable but doesn't seem to be passing it to powershell correctly. I also tried adding -Path %RecoveredDir% but that yielded an error (the param in report.ps1 is named $Path).
What am I doing incorrectly?
One potential problem is that not enclosing %RecoveredDir% in "..." would break with paths containing spaces and other special chars.
However, the bigger problem is that using mere file name RecoveredPath.txt means that the file is looked for in whatever the current directory happens to be.
In a comment your state that both the batch file and input file RecoveredPath.txt are located in your desktop folder.
However, it is not the batch file's location that matters, it's the process' current directory - and that is most likely not your desktop when your batch file auto-runs on startup.
Given that the batch file and the input file are in the same folder and that you can refer to a batch file's full folder path with %~dp0 (which includes a trailing \), modify your batch file to look as follows:
set /p RecoveredDir=<"%~dp0RecoveredPath.txt"
powershell.exe -File C:\...Report.ps1 "%RecoveredDir%"

Open a file with spaces in the pathname

I'm trying to use vscode (0.4.0) to open a file from the powershell commandline which has spaces in the path. Specifically the powershell profile, which lives under "My Documents"
I initially tried just:
code $PROFILE
but this just opens a file called "My" and does similar if I quote the file path:
code '\\myserver\myname\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'
code "\\myserver\myname\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"
how do I go about opening a file from the commandline when it has spaces in the path?
I have also tested it on a local file "C:\temp\Test Spaces\foobar.txt" and it does similar, so it's not related to UNC
This seems strange, but I tried double quotes, and it worked:
code ""C:\test\test file.js""

Restore Batchfile, VB script shortcut in new System XP

In my old system (XP) I created many shortcuts in a folder on my desktop to open programs like eclipse, notepad++, etc. and some script files. I assigned shortcuts like
Cnrl+Alt+E for Eclipse, Cnrl+Alt+N for notepad, etc.
I have backed up and restored the shortcuts folder on the desktop and batch/script file folder to my new system in the same path.
Is there any script to register all the shortcut to registry in one go?
The hotkey is a function of the file.
XP looks in 4 places to discover hotkeys:
%UserProfile%\desktop
%AllUsersProfile%\desktop
%UserProfile%\Start Menu
%AllUsersProfile%\Start Menu
and in these three file types:
*.lnk
*.pif
*.url
as long as the files are in those locations, the hotkeys should still work
This vbs script will show you the hotkey assigned to a file:
on error resume next
set WshShell = WScript.CreateObject("WScript.Shell" )
Set Arg=Wscript.Arguments
set lnk = WshShell.CreateShortcut(Arg)
If lnk.hotkey <> "" then
msgbox Arg & vbcrlf & lnk.hotkey
End If
use it with the filename passed to the script, e.g. thescript.vbs %UserProfile%\desktop\myshortcut.lnk

Open text file and program shortcut in a Windows batch file

I have two files in the same folder that I'd like to run. One is a .txt file, and the other is the program shortcut to an .exe. I'd like to make a batch file in the same location to open the text file and the shortcut then close the batch file (but the text file and program remain open).
I tried this with no luck:
open "myfile.txt"
open "myshortcut.lnk"
Also didn't work:
start "myfile.txt"
start "myshortcut.lnk"
I was able to figure out the solution:
start notepad "myfile.txt"
"myshortcut.lnk"
exit
This would have worked too. The first quoted pair are interpreted as a window title name in the start command.
start "" "myfile.txt"
start "" "myshortcut.lnk"
Don't put quotes around the name of the file that you are trying to open; start "myfile.txt" opens a new command prompt with the title myfile.txt, while start myfile.txt opens myfile.txt in Notepad. There's no easy solution in the case where you want to start a console application with a space in its file name, but for other applications, start "" "my file.txt" works.
The command-line syntax for opening a text file is:
type filename.txt
File types supported by this command include (but are not limited to): .doc, .txt, .html, .log
If the contents is too long, you can add "|more" after "type filename.txt", and it will pause after each screen; to end the command before the end of the file, you can hold Ctrl + C.
I use
#echo off
Start notepad "filename.txt"
exit
to open the file.
Another example is
#echo off
start chrome "filename.html"
pause
You can also do:
start notepad "C:\Users\kemp\INSTALL\Text1.txt"
The C:\Users\kemp\Install\ is your PATH. The Text1.txt is the FILE.
"location of notepad file" > notepad Filename
C:\Users\Desktop\Anaconda> notepad myfile
works for me! :)
In some cases, when opening a LNK file it is expecting the end of the application run.
In such cases it is better to use the following syntax (so you do not have to wait the end of the application):
START /B /I "MyTitleApp" "myshortcut.lnk"
To open a TXT file can be in the way already indicated (because notepad.exxe not interrupt the execution of the start command)
START notepad "myfile.txt"
The command start [filename] opened the file in my default text editor.
This command also worked for opening a non-.txt file.
If you are trying to open an application such as Chrome or Microsoft Word use this:
#echo off
start "__App_Name__" "__App_Path__.exe"
And repeat this for all of the applications you want to open.
P.S.: This will open the applications you select at once so don't insert too many.
Try using:
#ECHO off
ECHO Hello World!
START /MAX D:\SA\pro\hello.txt
Its very simple,
1)Just go on directory where the file us stored
2)then enter command i.e. type filename.file_extention
e.g type MyFile.tx
To open a file with default software just need to type the path of the file or, if you are at the file location, the file name.
C:\Users\MyName>C:\User\MyName\Desktop\hello.txt
or
C:\Users\MyName\Desktop>hello.txt
If you want specific program like notepad you can specify it first.
C:\Users\MyName>notepad C:\User\MyName\Desktop\hello.txt
or
C:\Users\MyName\Desktop>notepad hello.txt
Note that notepad is usually default text editor for .txt, in this case would make more sense to type notebook only to open a .cs/.cpp/.py file if your default for that files is any IDE and you just want to see the file on notebook
Regarding the batch file it will work the same way but to open them at the same time and let the command line go away you should use:
start "title" {filename}
So the command can open the file and return to next line immediately.
start "" C:\Users\MyName\MyFolder\foo.exe
start "" C:\Users\MyName\MyFolder\notes.txt
or
start "" foo.exe
start "" notes.txt
The last one only works if the batch file is on the same location of the files.
If you plan on using the console to open the batch file and you want the console to close at the end you should indeed write exit on last line.
When in doubt, it always helps to read the docs:
>help start
Starts a separate window to run a specified program or command.
START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
[/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
[command/program] [parameters]
"title" Title to display in window title bar.
path Starting directory.
B Start application without creating a new window. The
application has ^C handling ignored. Unless the application
enables ^C processing, ^Break is the only way to interrupt
the application.
I The new environment will be the original environment passed
to the cmd.exe and not the current environment.
MIN Start window minimized.
MAX Start window maximized.
SEPARATE Start 16-bit Windows program in separate memory space.
SHARED Start 16-bit Windows program in shared memory space.
LOW Start application in the IDLE priority class.
NORMAL Start application in the NORMAL priority class.
HIGH Start application in the HIGH priority class.
REALTIME Start application in the REALTIME priority class.
ABOVENORMAL Start application in the ABOVENORMAL priority class.
BELOWNORMAL Start application in the BELOWNORMAL priority class.
NODE Specifies the preferred Non-Uniform Memory Architecture (NUMA)
node as a decimal integer.
AFFINITY Specifies the processor affinity mask as a hexadecimal number.
Picture for the visual learners: