We find modular transformations of normalized characters for the following W-algebras: (a) \(W_k^{min}(\mathfrak {g}), \text {where } \mathfrak {g}=D_n (n\ge 4), \text {or } E_6, E_7, E_8,\) and k is a negative integer \(\ge -2\) , or \(\ge -\frac{h^\vee }{6}-1\) , respectively; (b) quantum Hamiltonian reduction of the \(\hat{\mathfrak {g}}\) -module \(L(k \Lambda _0)\) , where \(\mathfrak {g}\) is a simple Lie algebra, f is its non-zero nilpotent element, and k is a principal admissible level with the denominator \(u>\theta (x)\) , where 2x is the Dynkin characteristic of f, and \(\theta \) is the highest root of \(\mathfrak {g}\) . We prove that these vertex algebras are modular invariant. A conformal vertex algebra V is called modular invariant if its character \(tr_V q^{L_0-c/24}\) converges to a holomorphic modular function in the complex upper half-plane on a congruence subgroup. We find explicit formulas for their characters. Modular invariance of V is important since, in particular, conjecturally it implies that V is simple, and that V is rational, provided that it is lisse.