Categories
Powershell Windows 10 Windows 7

Running batch and powershell scripts as administrator

WINDOWS – RUN BATCH AND PS AS ADMINISTRATORS. This works for Windows 7 and newer. I tested on Windows 7 and 10 64bit and 32bit.

This is a faster vbs version of the below script:
net sess>NUL 2>&1||(echo.CreateObject^(“Shell.Application”^).ShellExecute”%~0″,,,”RunAs”,1 >”%TEMP%\%~nx0.vbs”&WScript “%TEMP%\%~nx0.vbs”&del “%TEMP%\%~nx0.vbs”&exit)

#RUNNING CMD/BAT files as administrator, place this at the top of script after @echo off
net sess>NUL 2>&1||(powershell saps ‘%0’-Verb RunAs&exit)

#RUNNING PS1 FILES as administrator, place this at the top, before all other commands.
# Run this if you need to be running as ADMINISTARTOR
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] “Administrator”))
{
$arguments = “& ‘” + $myinvocation.mycommand.definition + “‘”
Start-Process powershell -Verb runAs -ArgumentList $arguments
Break
}
####################################################

SOURCE: http://www.sevenforums.com/general-discussion/12936-how-run-batch-file-admin-3.html#post3084570

Categories
Powershell

Running a powershell command via batch or command that is restricted

I had to run a powershell module that I found remotely to remove some pinned applications in Windows 7. I also wanted it to run remotely, and not have to log on to each PC to do so. To kick off a powershell script, I had to add this to a command file:

REM KICK OFF POWERSHELL STUFF
xcopy "\\domain.com\software\Non-Licensed\scripts\kiosk\Set-PinnedApplications.psm1" C:\temp\
powershell "Set-ExecutionPolicy Unrestricted"
powershell \\domain.com\software\Non-Licensed\scripts\kiosk\Remove_Taskbar_Shortcuts.ps1

The .ps1 file leads to this:

Import-Module C:\temp\Set-PinnedApplications.psm1
Set-PinnedApplication -Action UnPinFromTaskBar -FilePath "C:\Users\kiosk\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Internet Explorer.lnk"
Set-PinnedApplication -Action UnPinFromTaskBar -FilePath "C:\Users\kiosk\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Windows Explorer.lnk"
Set-PinnedApplication -Action UnPinFromTaskBar -FilePath "C:\Users\kiosk\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Windows Media Player.lnk"

So it allows me to remove the pinned applications. If you want to view the module (Set-PinnedApplications.psm1) I used off technet it is here:
https://gallery.technet.microsoft.com/scriptcenter/b66434f1-4b3f-4a94-8dc3-e406eb30b750
You would just copy that content into: Set-PinnedApplications.psm1

Categories
Operating Systems Powershell

Windows 10 not searching desktop applications with cortona

Out of the blue, my start -> search stopped functioning, it would just be blank.

I reinstalled Cortana using the following procedure:

Open an elevated Command Prompt window (press win + X, and then press A)
Type start powershell and press enter
Run the command (in one line):
Get-AppXPackage -Name Microsoft.Windows.Cortana | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register “$($_.InstallLocation)\AppXManifest.xml”}
After 30 seconds the problem was solved on my machine. Incredible.

Source: http://answers.microsoft.com/en-us/windows/forum/windows_10-win_cortana/cortana-not-finding-desktop-apps-when-searching/f612e995-6664-4b91-b6ae-96790e763858

Categories
Powershell

Adding User Principal Names in Active Directory via PowerShell for Federation

I wanted to update the UPN or User Principal Names in our AD, as we had a couple thousand users that had been in our AD for over 10 years, in the NT days. So they were created without UPN’s.

This will print out the list of users and output it to a file so you can review who will be changed. We did not want to change the admin users so I added a notlike clause.

get-aduser -Filter * -SearchBase ‘CN=Users,dc=vivithemage,dc=com’ | where {($_.userprincipalname -eq $null) -and ($_.name -notlike “*admin*”)} | format-table samaccountname,givenname,surname | Out-File c:\test\UPN-prechange2.txt

Reviewed the list, looked good, so I can now run this to make the blanket change, while manually specifying the domain name:

get-aduser -Filter * -SearchBase ‘CN=Users,dc=vivithemage,dc=com’ | where {($_.userprincipalname -eq $null) -and ($_.name -notlike “*admin*”)} | foreach { Set-ADUser $_ -UserPrincipalName (“{0}@{1}” -f $_.name,”vivithemage.com”)}

Lots of help from this article: http://blogs.technet.com/b/heyscriptingguy/archive/2013/08/13/add-user-principal-names-in-active-directory-via-powershell.aspx
and ss64.com

Categories
Operating Systems Powershell Uncategorized Windows 7

copy a file into all user directories via bat files for windows 7 or xp using a wildcard

I needed to copy one file into all of the user directories on computers. I ended up creating a for loop, print it, then use that list as a variable to throw in, worked great. This was one of the few things I could not find on google, so hopefully this hits a few keywords for people when they’re searching. IT IS POSSIBLE! This can be done in BAT, CMD, OR just dump it into a command pronpt changing your own directories/variables as needed.

REM this prints all users in C:\Users\ and then copies the EssUser.cfg file to the PartsDoc Dir
FOR /D %%G IN (c:\Users\*.*) DO xcopy /Y /H /R "E:\PartsDoc Updates\EssUser.cfg" "%%G\Documents\CLAAS\PartsDoc\"