0

I have a ps1 (Powershell) file that i would like to run whenever a user logs into their account on Windows 10 i have tried other answers to similar questions but none of them seem to work. Is this possible on windows 10 or not? Thanks.

Powershell file:

(New-Object Media.SoundPlayer "C:\Users\Public\Public Recorded TV\Sample Media\System32.wav").PlaySync()
set-itemproperty -path "HKCU:Control Panel\Desktop" -name WallPaper -value TrollFace.jpg
[void][reflection.assembly]::LoadWithPartialName("System.Windows.Forms")

$file = (get-item 'C:\TrollFace.jpg')
#$file = (get-item "C:\TrollFace.jpg")
Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;
[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume
{
    // f(), g(), ... are unused COM method slots. Define these if you care
    int f(); int g(); int h(); int i();
    int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
    int j();
    int GetMasterVolumeLevelScalar(out float pfLevel);
    int k(); int l(); int m(); int n();
    int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
    int GetMute(out bool pbMute);
}
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice
{
    int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator
{
    int f(); // Unused
    int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
public class Audio
{
    static IAudioEndpointVolume Vol()
    {
        var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
        IMMDevice dev = null;
        Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
        IAudioEndpointVolume epv = null;
        var epvid = typeof(IAudioEndpointVolume).GUID;
        Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
        return epv;
    }
    public static float Volume
    {
        get { float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v; }
        set { Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty)); }
    }
    public static bool Mute
    {
        get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
        set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
    }
}
'@
while($true){
[audio]::Volume  = 1
[audio]::Mute = $false


$img = [System.Drawing.Image]::Fromfile($file);

# This tip from http://stackoverflow.com/questions/3358372/windows-forms-look-different-in-powershell-and-powershell-ise-why/3359274#3359274
[System.Windows.Forms.Application]::EnableVisualStyles();
$form = new-object Windows.Forms.Form
$form.Text = "Image Viewer"
$form.Width = $img.Size.Width;
$form.Height =  $img.Size.Height;
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Width =  $img.Size.Width;
$pictureBox.Height =  $img.Size.Height;

$pictureBox.Image = $img;
$form.controls.add($pictureBox)
$form.Add_Shown( { $form.Activate() } )
$form.ShowDialog()
}
2
  • 1
    What have you tried to do this? If you have access to the user account itself (you know the username/password) you can set up a scheduled task that will run on-logon.
    – Darius
    Jul 22, 2017 at 23:56
  • 1
    Have you tried the solutions given in this thread? superuser.com/questions/15596/… Jul 23, 2017 at 0:35

1 Answer 1

0

You can use windows task scheduler to run the file when the user logs in.

Instructions:

  • Press Winkey + R to open run window and then type Taskschd.msc and hit enter.
  • Under the "Action" menu (on the right) click on "Create Task"
  • Under "General" Tab name the task whatever you want ( You may want to check the "Run with highest privileges" checkbox if you are doing anything that requires admin privileges)

  • Under "Trigger" Tab Click "New"

  • Under "Begin the task" choose "At Logon"

  • Under Settings choose "Any User"

  • Press "OK"

  • Under "Action" Tab select "New"

  • Under "Action" select "Start a program"

  • Choose the location of the script under "Settings"

  • Click "OK"

  • Under "Conditions" Tab Deselect "Start the task only if Computer on AC power"

  • Under "Settings" Tab select "Run task as soon as possible after a scheduled start is missed"

  • Click "Ok"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .