How to set the ExecutionPolicy using Powershell

To set the ExecutionPolicy using Powershell, run Powershell as Administrator.
Type in Set-ExecutionPolicy followed by the ExecutionPolicy you wish to use.

  • Restricted: Restricted is the default execution policy. PowerShell scripts are not allowed to run.
  • All Signed: All Signed means thath if they are signed by a trusted publisher.
  • Remote Signed: Remote Signed means, any PowerShell scripts that have been locally created will be allowed to run.
  • Unrestricted: As the name implies, Unrestricted removes all restrictions from the execution policy.
  • Microsoft has disabled scripting by default to prevent malicious code from being executed.

    Please rate this snippet

    How to Log off, Restart or Shutdown Windows using Batch

    To Logoff, Restart or Shutdown Windows using Batch you can Simply execute shutdown with the needed parameters. To execute the shutdown command you need the sufficient user privileges.

    List of Parameters

  • -l Logs off the current user, this is also the default. -m ComputerName takes precedence.
  • -s Shuts down the local computer.
  • -r Reboots after shutdown.
  • -a Aborts shutdown. Ignores other parameters, except -l and ComputerName. You can only use -a during the time-out period.
  • -f Forces running applications to close. Note that if you are on a remote machine without using -f your session maybe closed but a hanging program blocks the Shutdown
  • -m [ \\ ComputerName ] Specifies the computer that you want to shut down.
  • -t xx Sets the timer for system shutdown in xx seconds. The default is 20 seconds.
  • -c ” message “ Specifies a message to be displayed in the Message area of the System Shutdown window. You can use a maximum of 127 characters. You must enclose the message in quotation marks.
  • -d [ u ][ p ] : xx : yy Lists the reason code for the shutdown. Reason Codes (u=Indicates a user code), (p=Indicates a planned shutdown cod), (xx=Specifies the major reason code (0-255)), (yy=Specifies the minor reason code (0-65536)
  • /? Displays help at the command prompt.
  • FOR MORE INFORMATIONS AND DETAILS TAKE A LOOK AT THE MSDN.

    Please rate this snippet

    How to hide a Console Window in C# and VB.NET

    To hide a Console Window in C# and VB.NET you can use the following snippet.

    Sample C#

    public static class Consoles
    {
    	[DllImport("user32.dll")]
    	public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
    	[DllImport("user32.dll")]
    	public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
    	public static void SetConsoleWindowVisibility(bool visible, IntPtr hWnd)
    	{
    		ShowWindow(hWnd, !visible ? 0 : 1);
    	}
    }
    

    usage:

    var hWnd = Consoles.FindWindow(null, Console.Title);
    Consoles.SetConsoleWindowVisibility(false, hWnd);
    

    Sample VB.NET

    Public NotInheritable Class Consoles
    	Private Sub New()
    	End Sub
    	<DllImport("user32.dll")> _
    	Public Shared Function FindWindow(lpClassName As String, lpWindowName As String) As IntPtr
    	End Function
    
    	<DllImport("user32.dll")> _
    	Public Shared Function ShowWindow(hWnd As IntPtr, nCmdShow As Integer) As Boolean
    	End Function
    
    	Public Shared Sub SetConsoleWindowVisibility(visible As Boolean, hWnd As IntPtr)
    		ShowWindow(hWnd, If(Not visible, 0, 1))
    	End Sub
    End Class
    

    usage:

    Dim hWnd = Consoles.FindWindow(Nothing, Console.Title)
    Consoles.SetConsoleWindowVisibility(False, hWnd)
    

    Please rate this snippet

    How to show and select a file in explorer using C# and VB.NET

    To show and select a file in explorer using C# and VB.NET you can use the following snippet.

    Sample C#

    private static void ShowFileInExplorer(string filePath)
    {
    	try
    	{
    		var winDir = Environment.GetEnvironmentVariable("windir");
    		if (winDir != null)
    		{
    			var explorerPath = Path.Combine(winDir, @"explorer.exe");
    			var arguments = String.Format("/select, {0}{1}{0}", (char)34, filePath);
    			Process.Start(explorerPath, arguments);
    		}
    	}
    	catch (Exception ex)
    	{
    		//handle the exception your way!
    	}
    }
    

    Sample VB.NET

    Private Shared Sub ShowFileInExplorer(filePath As String)
    	Try
    		Dim winDir = Environment.GetEnvironmentVariable("windir")
    		If winDir IsNot Nothing Then
    			Dim explorerPath = Path.Combine(winDir, "explorer.exe")
    			Dim arguments = [String].Format("/select, {0}{1}{0}", Char(34), filePath)
    			Process.Start(explorerPath, arguments)
    		End If
    	Catch ex As Exception
    	        'handle the exception your way!
            End Try
    End Sub
    

    Please rate this snippet

    How to show and select a file in explorer using VBA

    To show and select a file in explorer using VBA you can use the following snippet.
    This snippet should also work in VB6.

    Sample VBA

    Dim path As String
    Dim explorerPath As String
    Dim cmd As String
    
    explorerPath = Environ$("WINDIR") + "\Explorer.exe "
    path = "C:\Users\Codesnippets\Desktop\test\Test.xml"
    cmd = explorerPath + " /select , " + Chr(34) + path + Chr(34)
    Call Shell(cmd, vbNormalFocus)
    

    Please rate this snippet

    How to open a file or folder using VBA

    To open a file or folder using VBA you can sue the follwing snippet.
    This snippet should also work in VB6.

    Sample VBA

    Dim path As String
    Dim explorerPath As String
    
    explorerPath = Environ$("WINDIR") + "\Explorer.exe "
    path = "C:\Users\Codesnippets\Desktop\test"
    Call Shell(explorerPath + Chr(34) + path + Chr(34), vbNormalFocus)
    

    Please rate this snippet

    How to delete a file or folder in Powershell

    To delete a file or Folder in Powershell you can use the following snippet.

    Sample Powershell

    if you want to delete a file use

    remove-item -path "C:\Users\Codesnippets\Desktop\Test.xml" -force -recurse
    

    if you want to delete a folder use

    remove-item -path "C:\Users\Codesnippets\Desktop\Folder1" -force -recurse
    

    for more informations see Using the Remove-Item Cmdlet

    Please rate this snippet

    How to start a process and wait for exit in Powershell

    To start a application and wait for exit in Powershell you can use the following snippet.

    Sample Powershell

    start-process -filepath "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe" -Wait -NoNewWindow -argumentlist '-args'
    

    for more informations take a look at the MSDN Start-Process

    Please rate this snippet

    How to check if file or folder exists in Powershell

    To check if file or folder exists in Powershell you can use the following snippet.

    Sample Powershell

    for files use it like this

    if(test-path "C:\Users\Codesnippets\Desktop\Test.xml") 
    { 
    	echo "exists!" 
    } 
    else 
    { 
    	echo "not exists!" 
    }
    

    for folders/directories use it like this

    if(test-path "C:\Users\Codesnippets\Desktop") 
    { 
    	echo "exists!" 
    } 
    else 
    { 
    	echo "not exists!" 
    }
    

    Please rate this snippet