Create " *.url " file using powershell and CMD - powershell

i was inquiring if there are options to create a url shortcut to be placed on c:\users\username\favorites to be applied to be added to the login script of the user on Active directory to create the favorites for any user
i tried searching all over the internet and i only found "MD" and "MKDir" commands

Not the most PowerShell answer but this is the widely used one with the best compatibility in versions. Much like this answer but small changes for URLs
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$Home\Favorites\Google.url")
$Shortcut.TargetPath = "http://www.google.ca"
$Shortcut.Save()
This will make a shortcut to Google in your favorites. $Home being one of PowerShell's Automatic Variables
If you happen to have PowerShell 5.0 it can now do this natively. Again refer to this answer for more information

Related

Boxstarter or PowerShell command to change "Opens With" properties

I'm trying to develop my own Boxstarter script for spinning up new machines. I just realized that I'd really like to add a line that will change default applications to open certain file types. For example, I want to open .txt files with Notepad++. I know how to do this by right-click the file and checking it's properties, but is there a line I can add to my Boxstarter script that will do it? Or, since Boxstarter is basically a special set of PowerShell commands, is there a PowerShell command I can invoke directly to change the opens with property? I did some searching, and most of the results were about how to get PowerShell to open something, not change the opens with property. The rest were all about how to open PowerShell.
Another similar, but not quite the same, way to go about this is to change the file association you want to associate with a particular applicaition. Chocolatey includes some helper commands to do this and is therefore available to your Boxstarter package. Here is an excerpt from one of my Boxstarter packages:
Install-ChocolateyFileAssociation ".txt" "$env:programfiles\Sublime Text 2\sublime_text.exe"
Install-ChocolateyFileAssociation ".dll" "$($Boxstarter.programFiles86)\jetbrains\dotpeek\v1.1\Bin\dotpeek32.exe"
So now double clicking on any text file opens sublime or any dll opens dotpeek.
But I agree. Its still helpful to be able to add to the "Open With..." list.
Thanks to #Raf for pointing me in the right direction. Here's the code to change the OpensWith property of .txt files:
$principal = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
$key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice",[Microsoft.Win32.RegistryKeyPermissionCheck]::ReadWriteSubTree,[System.Security.AccessControl.RegistryRights]::ChangePermissions)
$acl = $key.GetAccessControl()
$right = "SetValue"
$denyrule = New-Object System.Security.AccessControl.RegistryAccessRule($principal,$right,"DENY")
$ret = $acl.RemoveAccessRule($denyrule)
$ret = $key.SetAccessControl($acl)
Set-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\UserChoice -Name ProgId -Value Applications\notepad++.exe
Slightly modified from an answer in the TechNet forums.
I haven't figured out if there's a boxstarter shortcut for this, but changing the ACL rules was the key. Without it, you don't have the proper access to change this particular registry item. Even when I tried running Powershell as Admin and made sure I had all the right permissions on the UserChoice key (both the administrator account and my user account had Full Control), I kept getting an error that the Requested registry access is not allowed.

Add mstsc to shortcut target powershell

A little background about what I'm trying to do and why. We are slowly migrating from using a local copy of office on our end users Win7 machines to publishing office through RDS 2012. With 2012 you can have the end users machine subscribe to a webfeed which puts the shortcuts to the actual RDP files in Appdata. In order to have our image pretty much mirror before RDS, I need to pin the shortcuts to the taskbar. If you pin the shortcut as it comes from the RDS Gateway server the icon on the taskbar is that of the .rdp file. If you edit the target for the shortcut and put mstsc.exe before the path to the .rdp file you can then pin the shortcut to the taskbar using those icons of the shortcut.
I have found posts on how to change the target field of shortcuts but nothing on how to add something to what is currently there. An environment variable is needed since the path to the shorts will be different for each user. Below is I have tried thus far
$Word = $env:userprofile + "\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\RemoteApp and Desktop Connections\Microsoft Word 2010.lnk"
$sh = New-Object -COM WScript.Shell
$targetPath = $sh.CreateShortcut($Word).TargetPath
$sh.TargetPath = "mstsc.exe" + $targetPath ## Make changes
$sh.Save() ## Save$shell = New-Object -COM WScript.Shell
One of the errors i'm getting is : Property 'Arguments' cannot be found on this object; make sure it exists and is settable.
Any help will be greatly appreciated.
Instead of using Shell COM object, how about using a .Net wrapper class? There is a great sample.
To use the VBAccelerator's wrapper in Powershell, extract the source code and compile a DLL like so,
C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /t:library /out:ShellLink.dll /r:System.Drawing.dll .\ShellLink.cs .\FileIcon.cs
This should create ShellLink.dll, which you can add as Powershell type like so,
Add-Type -path .\ShellLink.dll
And use the class by creating a new object like so,
$lnk = new-object vbAccelerator.Components.Shell.ShellLink
$lnk.Target = "C:\Windows\System32\mstsc.exe"
$lnk.Arguments = "some arguments"
$lnk.Description = "My awesome shortcut"
$lnk.Save("c:\temp\test.lnk")
Your code will prepend "mstsc.exe" to the current target path if you just add this line before your ## Make changes line:
$sh = $sh.CreateShortcut($word)
Sounds like you also want to add a space, so that your lnk file is the "connection file" argument of mstsc.exe. You can set the Arguments property like so:
function Set-RDSshortcut {
param( [parameter(mandatory=$true)]$Shortcut )
$sh = New-Object -COM WScript.Shell
$targetPath = $sh.CreateShortcut($Shortcut).TargetPath
$targetPath = "mstsc.exe"
$sh = $sh.CreateShortcut($Shortcut)
$sh.TargetPath = $targetPath
$sh.Arguments = $Shortcut
$sh.Save()
} #end function

creating a shortcut on a remote desktop, but the shortcut is created with a "file" instead of "file folder" as the "target type"

I'm trying to create a shortcut on a remote desktop in the domain here, and I'm a domain admin. If I run the following code directly on the target machine, the shortcut can be created and is able to lead me to the target path.
$shortcutpath3 = "c:\Users\Public\Desktop\Shortcuts to Test Custom\VV 1211 -TC.lnk"
$WshShell3 = New-Object -comObject WScript.Shell
$Shortcut3 = $WshShell3.CreateShortcut($shortcutpath3)
$Shortcut3.TargetPath = "\\machine\testcustom\"
$Shortcut3.Save()
I saved this script as test.ps1, run it with folloing code on a different mahchine. The code ends without any errors/warings, and the shortcut is created on the target machine with the propeties i specified. But it cannot lead me to the target place, it actually ask me to pick a program to open that file. I compared the properties of the 2 shortcuts, and found that the "target type" of the broken shortcut is "file" while it is "file folder" for a good shortcut.
Invoke-Command -ComputerName TARGETSERVER -FilePath test.ps1
Any idea how i can fix this? And why is this happening? Thank!!!
I had the same problem and and I used Get-Item to make it work. Try this:
$targetPath = Get-Item("\\machine\testcustom\")
$WshShell3 = New-Object -comObject WScript.Shell
$Shortcut3 = $WshShell3.CreateShortcut($shortcutpath3)
$Shortcut3.TargetPath = $targetPath.FullName
$Shortcut3.Save()
Since you're a domain admin I'd strongly recommend doing this with a Group Policy Preference. You can restrict shortcut creation to particular users/groups/computers/etc. via item-level targeting.
I've been battling this for the past several hours, Googling to no avail. For posterity, here's my summary. While alternatives suggestions are appreciated:
PowerShell inexplicably has no direct way to create a shortcut. It can create symbolic link, but 1) it requires admin rights, and 2) it behaves differently.
Group Policy Preferences are great--but only if your machines are in office or routinely on a VPN.
If you're trying to create a shortcut to a network folder, setting the TargetPath to that folder only works if the computer actually can reach it when the script runs, i.e. same issue as with Group Policy Preferences. PowerShell will create the shortcut, but the Target Type will be a File rather than a File Folder (and I found no info online how to control that; trailing slash or not doesn't matter).
The behavior of running these code suggestions varies depending whether you run this interactively or as a script.
Here's what I found does work:
Set the $shortcut.TargetPath to "C:\Windows\Explorer.exe"
Add a line: $shortcut.Arguments = """Target folder""" (the """ are needed as an escape character for the shortcut to show as C:\Windows\Explorer.exe "Target folder")
So for your example above, the following should work (assuming permissions to c:\Users\Public\Desktop):
$shortcutpath3 = "c:\Users\Public\Desktop\Shortcuts to Test Custom\VV 1211 -TC.lnk"
$WshShell3 = New-Object -comObject WScript.Shell
$Shortcut3 = $WshShell3.CreateShortcut($shortcutpath3)
$Shortcut3.TargetPath = "C:\Windows\Explorer.exe"
$shortcut.Arguments = """\\machine\testcustom\"""
$Shortcut3.Save()

Display progress of exe installation from powershell?

I'm installing a file through powershell silently, but would like to give feedback on the progress of the installation. I can't seem to find this information anywhere. This is the code I have for running the exe:
$exe = "wls1033_oepe111150_win32.exe"
$xmlLocation = Resolve-Path "silent_install.xml"
$xmlLocation = "-silent_xml=" + $xmlLocation
$installLogLoc = Resolve-Path "wls_install.log"
$installLogLoc = "-log=" + $installLogLoc
$AllArgs = #('-mode=silent', $xmlLocation, $installLogLoc)
$filePath = Resolve-Path $exe
$p = New-Object System.Diagnostics.Process
$p.StartInfo.Filename = $filePath
$p.StartInfo.Arguments = $AllArgs
$p.Start();
$p.WaitForExit();
Is there even a way to do this? I do get a progress meter for the extraction process in an alternate command window that installs the exe, but other than that it sits for there about 10 minutes with no sort of indication.
Edit: So seeing as this isn't possible, is there a way to do an asynchronous pipeline call while running the exe?
Thanks
Please correct me if I'm wrong, but I'm 99% sure that this is NOT possible. Your exe file is seperate process and not a PowerShell script. It will not be able to pass status messages to your PowerShell sessions. The only possibility would be to detect the setups log-file, tail it and update a progressbar in PowerShell based on keywords from the log. This is however a big task and something you need to customize for each setup file.
I would try to look into your exe file and see if it has a "basic ui" (or a similar type) mode that you can use instead of the silent option. A last alternative would be to repackage the setup with such an option(making installation automated with only progressbar). This solution would still only show the progressbar in a separate window and not in PowerShell itself.

Open url from script on Windows Server 2008

I want to write a script that loads a url (eg. http://google.com) automatically. But I don't want to install any 3rd party libraries or programs to the server. what's the easiest way to do this?
I just my options are batch script, vb script or powershell right?
FYI from PowerShell, if you want to retrieve the contents of the URL you can do this;
$page = (new-object net.webclient).DownloadString("http://www.bing.com")
$page # writes the contents to the console
If you just want to open it in a browser:
Start-Process http://www.bing.com
Or using the start alias
start http://www.bing.com
Start-Process is new in PowerShell 2.0.
The beauty of Powershell is it has so many ways to do something.
This is my Powershell 2.0 example code - consisting of a Pause function to allow the site to open. It uses Internet Explorer as the browser. In this case - IE is a better browser than the others because it integrates with Powershell through a verbose API.
$url = "http://www.google.com/"
$ie = new-object -com "InternetExplorer.Application"
$ie.Navigate($url)
There are many different functions attached to this object. I recommend loading up the Powershell command line, typing in the above code, and checking what other functions this object has. Type $ie. and pressing TAB iterates through all the methods of this library.
The more I learn of Powershell, the more exciting it becomes. There is nothing it cannot do on Windows.
you can use vbscript
url="http://somewhere.com"
Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
objHTTP.Open "GET", url, False
objHTTP.Send
wscript.Echo objHTTP.ResponseText
objFile.Close