Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

16 August 2019

User Powershell Script as Startup or Login Script and Bypass Execution Policy

Use the following settings in the Login/Startup Script section of your GPO

Script Name: %windir%\System32\WindowsPowerShell\v1.0\powershell.exe
Script Parameters: -Noninteractive -ExecutionPolicy Bypass –Noprofile -file MyPSScript.ps1 -psscriptparameter1 value -pssscriptparameter2


-Noninteractive Prevents an interactive window from trying to open at any time during the script execution
-ExecutionPolicy Bypass Enables this single script to run if more restrictive Powershell Execution policies are normally applied on the target computer
-Noprofile Ensures PowerShell does not execute profile scripts and instead launches the desired script immediately in an unaltered environment

09 May 2014

Remove Server Manager and PowerShell from Taskbar on RDS

Best article on this is at

http://www.emware.nl/articles/remove-server-manager-and-powershell-icons-from-taskbar.html

The only thing I would add is to also include the following files



  • %AllUsersProfile%\Microsoft\Windows\Start Menu\Programs\Accessories\Windows PowerShell\Windows PowerShell (x86).lnk
  • %AllUsersProfile%\Microsoft\Windows\Start Menu\Programs\Administrative Tools\Windows PowerShell (x86).lnk
  • %AllUsersProfile%\Microsoft\Windows\Start Menu\Programs\Windows System\Windows PowerShell.lnk
  • %AllUsersProfile%\Microsoft\Windows\Start Menu\Programs\System Tools\Windows PowerShell.lnk


  • References:

    06 May 2014

    Installing Exchange 2010 SP3 on Windows 2012 (NOT R2)

    There are plenty of easily found documents on how to do this, so I'm not going to repeat the instructions here. However, I ran into an issue that none of those articles mentioned, the details of which are below.

    I had installed all of the various Exchange 2010 SP3 prerequisites on Windows Server 2012 and followed all of the articles exactly, but whenever I ran setup.exe (GUI) or setup.com (CLI) to install Exchange, I got the an error containing the following phrase:

    Could not load file or assembly 'System.Management.Automation' or one of its dependencies.
    
    
    If you encounter the error message, you simply need to use Server Management Console to install the Windows PowerShell 2.0 Engine feature.



    References:
    Standard install instructions for Exchange 2010 on Windows 2012 (NOT R2)
    http://oxfordsbsguy.com/2013/03/24/how-to-install-exchange-2010-sp3-on-windows-server-2012/

    Installing the Windows PowerShell 2.0 Engine
    http://technet.microsoft.com/en-us/library/hh849675.aspx

    The forum discussions that gave me a clue as to the PowerShell version issue
    http://stackoverflow.com/questions/15473734/could-not-load-file-or-assembly-system-management-automation


    19 February 2013

    Get Exchange Mailbox Stats

    Get-MailboxStatistics

    Get-MailboxStatistics | ft DisplayName,TotalItemSize,ItemCount


    Get-MailboxStatistics | Sort-Object TotalItemSize –Descending | ft DisplayName,TotalItemSize,ItemCount

    References:
    http://www.msexchange.org/articles-tutorials/exchange-server-2007/management-administration/getting-mailbox-statistics-exchange-2007.html

    26 October 2012

    Find Old Computers in AD

    For Windows 2003/2008
    (Must be 2003 Native domain or newer)
    In a Command Prompt window type:
    dsquery computer -inactive <num>
    Where <num> is the minimum number of weeks the device has been inactive for. Advice seems to be to use at least 2, as anything less than that is not fully reliable.

    For Windows 2008 R2+
    In a PowerShell  window type:
    $time=Read-host "Enter a date in format mm/dd/yyyy"
    then
    Get-ADComputer -Filter * | Get-ADObject -Properties lastlogontimestamp | where {(([DateTime]::FromFileTime($_.lastlogontimestamp) - ([system.datetime]$time)).totaldays) -lt 0 } | select name

    You can change what you include in the select command at the end to get more or different info.

    If you want to then remove all resulting computer accounts use the following command:
    Remove-ADObject -recursive
    in place of the select name

    That is, to remove all computers older than $time use the following command:
    Get-ADComputer -Filter * | Get-ADObject -Properties lastlogontimestamp | where {(([DateTime]::FromFileTime($_.lastlogontimestamp) - ([system.datetime]$time)).totaldays) -lt 0 } | Remove-ADObject -recursive
    For more detail or a better example (of which this is basically a cut and paste at the moment), see reference site below.

    References:
    http://blog.mattvogt.net/powershell-last-logon-timestamp-for-single-ho 

    10 October 2012

    Find the Desktop

    It can be REALLY hard to access the desktop using scripts, especially batch scripts, if it has been moved from the default location of C:\Users\%username%, or even worse if it has been moved out of the %userprofile% location by the way of Folder Redirection.

    As batch files cannot find this location, and I do not want to start learning VB script, I want a Powershell method I can use to find it.

    This is the method I found:

    $desktop = [Environment]::GetFolderPath("Desktop")

    The $desktop variable now contains the location to the currently logged-in user's desktop. To view this path simply type

    write-host $desktop

    Happy day!

    Reference:
    The comment on the post at http://stackoverflow.com/questions/11349885/get-currently-logged-in-users-with-powershell-to-add-shortcut-to-desktop

    :-)