Advertisement
Guest User

Untitled

a guest
Feb 11th, 2019
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Set-OSCPin
  2. {
  3.  
  4. <#
  5.  
  6. .SYNOPSIS
  7.  
  8.       Set-OSCPin is an advanced function which can be used to pin a item or more items to the Start menu.
  9.  
  10.   .DESCRIPTION
  11.  
  12.       Set-OSCPin is an advanced function which can be used to pin a item or more items to the Start menu.
  13.  
  14.   .PARAMETER  <Path>
  15.  
  16. Specifies a path to one or more locations.
  17.  
  18.   .EXAMPLE
  19.  
  20.       C:\PS> Set-OSCPin -Path "C:\Windows"
  21.  
  22.       Pin "Windows" to the Start menu sucessfully.
  23.  
  24. This command shows how to pin the "shutdown.exe" file to the Start menu.
  25.  
  26.   .EXAMPLE
  27.  
  28.       C:\PS> Set-OSCPin -Path "C:\Windows","C:\Windows\System32\shutdown.exe"
  29.  
  30.       Pin "Windows" to the Start menu sucessfully.
  31.  
  32.       Pin "shutdown.exe" to the Start menu sucessfully.
  33.  
  34. This command shows how to pin the "Windows" folder and "shutdown.exe" file to the Start menu.
  35.  
  36. #>
  37.  
  38.   [CmdletBinding()]
  39.  
  40.   Param
  41.  
  42.   (
  43.  
  44.       [Parameter(Mandatory,Position=0)]
  45.  
  46.       [Alias('p')]
  47.  
  48.       [String[]]$Path
  49.  
  50.   )
  51.  
  52.   $Shell = New-Object -ComObject Shell.Application
  53.  
  54. $Desktop = $Shell.NameSpace(0X0)
  55.  
  56.   $WshShell = New-Object -comObject WScript.Shell
  57.  
  58.   $Flag=0
  59.  
  60.   Foreach($itemPath in $Path)
  61.  
  62.   {
  63.  
  64.       $itemName = Split-Path -Path $itemPath -Leaf
  65.  
  66.       #pin application to windows Start menu
  67.  
  68.       $ItemLnk = $Desktop.ParseName($itemPath)
  69.  
  70.       $ItemVerbs = $ItemLnk.Verbs()
  71.  
  72.       Foreach($ItemVerb in $ItemVerbs)
  73.  
  74.       {
  75.  
  76.           If($ItemVerb.Name.Replace("&","") -match "Pin to Start")
  77.  
  78.           {
  79.  
  80.               $ItemVerb.DoIt()
  81.  
  82.               $Flag=1
  83.  
  84.           }
  85.  
  86.       }
  87.  
  88.       If($Flag=1)
  89.  
  90.       {
  91.  
  92.           Write-Host "Pin ""$ItemName"" to the Start menu sucessfully." -ForegroundColor Green
  93.  
  94.       }
  95.  
  96.       Else
  97.  
  98.       {
  99.  
  100.           Write-Host "The ""$ItemName"" cannot pin to the Start menu." -ForegroundColor Red
  101.  
  102.       }
  103.  
  104.   }
  105.  
  106. }
  107.  
  108. function Pin-App ([string]$appname, [switch]$unpin, [switch]$start, [switch]$taskbar, [string]$path) {
  109.    if ($unpin.IsPresent) {
  110.  
  111.        $action = "Unpin"
  112.  
  113.    } else {
  114.  
  115.        $action = "Pin"
  116.  
  117.    }
  118.  
  119.    if (-not $taskbar.IsPresent -and -not $start.IsPresent) {
  120.  
  121.        Write-Error "Specify -taskbar and/or -start!"
  122.  
  123.    }
  124.  
  125.    if ($taskbar.IsPresent) {
  126.  
  127.        try {
  128.  
  129.            $exec = $false
  130.  
  131.            if ($action -eq "Unpin") {
  132.  
  133.                ((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() | ?{$_.Name -eq $appname}).Verbs() | ?{$_.Name.replace('&','') -match 'Unpin from taskbar'} | %{$_.DoIt(); $exec = $true}
  134.  
  135.                if ($exec) {
  136.  
  137.                    Write "App '$appname' unpinned from Taskbar"
  138.  
  139.                } else {
  140.  
  141.                    if (-not $path -eq "") {
  142.  
  143.                        Pin-App-by-Path $path -Action $action
  144.  
  145.                    } else {
  146.  
  147.                        Write "'$appname' not found or 'Unpin from taskbar' not found on item!"
  148.  
  149.                    }
  150.  
  151.                }
  152.  
  153.            } else {
  154.  
  155.                ((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() | ?{$_.Name -eq $appname}).Verbs() | ?{$_.Name.replace('&','') -match 'Pin to taskbar'} | %{$_.DoIt(); $exec = $true}
  156.  
  157.                if ($exec) {
  158.  
  159.                    Write "App '$appname' pinned to Taskbar"
  160.  
  161.                } else {
  162.  
  163.                    if (-not $path -eq "") {
  164.  
  165.                        Pin-App-by-Path $path -Action $action
  166.  
  167.                    } else {
  168.  
  169.                        Write "'$appname' not found or 'Pin to taskbar' not found on item!"
  170.  
  171.                    }
  172.  
  173.                }
  174.  
  175.            }
  176.  
  177.        } catch {
  178.  
  179.            Write-Error "Error Pinning/Unpinning $appname to/from taskbar!"
  180.  
  181.        }
  182.  
  183.    }
  184.  
  185.    if ($start.IsPresent) {
  186.  
  187.        try {
  188.  
  189.            $exec = $false
  190.  
  191.            if ($action -eq "Unpin") {
  192.  
  193.                ((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() | ?{$_.Name -eq $appname}).Verbs() | ?{$_.Name.replace('&','') -match 'Unpin from Start'} | %{$_.DoIt(); $exec = $true}
  194.  
  195.                if ($exec) {
  196.  
  197.                    Write "App '$appname' unpinned from Start"
  198.  
  199.                } else {
  200.  
  201.                    if (-not $path -eq "") {
  202.  
  203.                        Pin-App-by-Path $path -Action $action -start
  204.  
  205.                    } else {
  206.  
  207.                        Write "'$appname' not found or 'Unpin from Start' not found on item!"
  208.  
  209.                    }
  210.  
  211.                }
  212.  
  213.            } else {
  214.  
  215.                ((New-Object -Com Shell.Application).NameSpace('shell:::{4234d49b-0245-4df3-b780-3893943456e1}').Items() | ?{$_.Name -eq $appname}).Verbs() | ?{$_.Name.replace('&','') -match 'Pin to Start'} | %{$_.DoIt(); $exec = $true}
  216.  
  217.                if ($exec) {
  218.  
  219.                    Write "App '$appname' pinned to Start"
  220.  
  221.                } else {
  222.  
  223.                    if (-not $path -eq "") {
  224.  
  225.                        Pin-App-by-Path $path -Action $action -start
  226.  
  227.                    } else {
  228.  
  229.                        Write "'$appname' not found or 'Pin to Start' not found on item!"
  230.  
  231.                    }
  232.  
  233.                }
  234.  
  235.            }
  236.  
  237.        } catch {
  238.  
  239.            Write-Error "Error Pinning/Unpinning $appname to/from Start!"
  240.  
  241.        }
  242.  
  243.    }
  244.  
  245. }
  246.  
  247. function Pin-App-by-Path([string]$Path, [string]$Action, [switch]$start) {
  248.  
  249.    if ($Path -eq "") {
  250.  
  251.        Write-Error -Message "You need to specify a Path" -ErrorAction Stop
  252.  
  253.    }
  254.  
  255.    if ($Action -eq "") {
  256.  
  257.        Write-Error -Message "You need to specify an action: Pin or Unpin" -ErrorAction Stop
  258.  
  259.    }
  260.  
  261.    if ((Get-Item -Path $Path -ErrorAction SilentlyContinue) -eq $null){
  262.  
  263.        Write-Error -Message "$Path not found" -ErrorAction Stop
  264.  
  265.    }
  266.  
  267.    $Shell = New-Object -ComObject "Shell.Application"
  268.  
  269.    $ItemParent = Split-Path -Path $Path -Parent
  270.  
  271.    $ItemLeaf = Split-Path -Path $Path -Leaf
  272.  
  273.    $Folder = $Shell.NameSpace($ItemParent)
  274.  
  275.    $ItemObject = $Folder.ParseName($ItemLeaf)
  276.  
  277.    $Verbs = $ItemObject.Verbs()
  278.  
  279.    if ($start.IsPresent) {
  280.  
  281.        switch($Action){
  282.  
  283.            "Pin"   {$Verb = $Verbs | Where-Object -Property Name -EQ "&Pin to Start"}
  284.  
  285.            "Unpin" {$Verb = $Verbs | Where-Object -Property Name -EQ "Un&pin from Start"}
  286.  
  287.            default {Write-Error -Message "Invalid action, should be Pin or Unpin" -ErrorAction Stop}
  288.  
  289.        }
  290.  
  291.    } else {
  292.  
  293.        switch($Action){
  294.  
  295.            "Pin"   {$Verb = $Verbs | Where-Object -Property Name -EQ "Pin to Tas&kbar"}
  296.  
  297.            "Unpin" {$Verb = $Verbs | Where-Object -Property Name -EQ "Unpin from Tas&kbar"}
  298.  
  299.            default {Write-Error -Message "Invalid action, should be Pin or Unpin" -ErrorAction Stop}
  300.  
  301.        }
  302.  
  303.    }
  304.  
  305.    if($Verb -eq $null){
  306.  
  307.        Write-Error -Message "That action is not currently available on this Path" -ErrorAction Stop
  308.  
  309.    } else {
  310.  
  311.        $Result = $Verb.DoIt()
  312.  
  313.    }
  314.  
  315. }
  316.  
  317. <#
  318. Pin-App "Microsoft Edge" -unpin -taskbar
  319. Pin-App "Store" -unpin -taskbar
  320. Pin-App "Fresh Paint" -unpin -start
  321. Pin-App "Wunderlist" -unpin -start
  322. Pin-App "Network Speed Test" -unpin -start
  323. Pin-App "Microsoft Edge" -unpin -start
  324. Pin-App "Alarms & Clock" -unpin -start
  325. Pin-App "Remote Desktop" -unpin -start
  326. Pin-App "Voice Recorder" -unpin -start
  327. Pin-App "Bing Wikipedia Browser" -unpin -start
  328. Pin-App "Photos" -unpin -start
  329. Pin-App "Calculator" -unpin -start
  330. Pin-App "Maps" -unpin -start
  331. Pin-App "Microsoft Store" -unpin -taskbar
  332.  
  333. Set-OSCPin -Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Outlook 2016.lnk"
  334. Set-OSCPin -Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Word 2016.lnk"
  335. Set-OSCPin -Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Excel 2016.lnk"
  336. Set-OSCPin -path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Skype for Business 2016.lnk"
  337. Set-OSCPin -path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Mozilla Firefox.lnk"
  338. Set-OSCPin -path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Google Chrome.lnk"
  339. Set-OSCPin -path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Remote Desktop Connection.lnk"
  340. Set-OSCPin -path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\OpenVPN Connect.lnk"
  341. Set-OSCPin -path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\OneNote 2016.lnk"
  342. Set-OSCPin -Path "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PowerPoint 2016.lnk"
  343.  
  344. Pin-App "Mail" -unpin -start
  345. Pin-App "Store" -unpin -taskbar
  346. Pin-App "Calendar" -unpin -start
  347. Pin-App "Microsoft Edge" -unpin -start
  348. Pin-App "Microsoft Edge" -unpin -taskbar
  349. Pin-App "Photos" -unpin -start
  350. Pin-App "Cortana" -unpin -start
  351. Pin-App "Cortana" -unpin -taskbar
  352. Pin-App "Weather" -unpin -start
  353. Pin-App "Phone Companion" -unpin -start
  354. Pin-App "Music" -unpin -start
  355. Pin-App "xbox" -unpin -start
  356. Pin-App "movies & tv" -unpin -start
  357. Pin-App "microsoft solitaire collection" -unpin -start
  358. Pin-App "money" -unpin -start
  359. Pin-App "get office" -unpin -start
  360. Pin-App "onenote" -unpin -start
  361. Pin-App "news" -unpin -start
  362. Pin-App "Groove Music" -unpin -start
  363. Pin-App "Sway" -unpin -start
  364. #>
  365.  
  366. Pin-App "Skype" -unpin -start
  367. Pin-App "Microsoft Edge" -unpin -taskbar
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement